MoonDarius
MoonDarius

Reputation: 61

Take id from one table and insert in another same time

I want to insert in the security the same id from users:

<?php

if (isset($_POST['reg_user'])) {
    require 'db.php';

    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password_1'];
    $passwordRepeat = $_POST['password_2'];
    $firstName = $_POST['firstname'];
    $lastName = $_POST['lastname'];
    $country = $_POST['country'];
    $city = $_POST['city'];
    $address = $_POST['address'];
    $zipCode = $_POST['zipCode'];

    if (empty($username) || empty($email) || empty($password) || empty($passwordRepeat) || empty($firstName) || empty($lastName) || empty($country) || empty($city) || empty($address) || empty($zipCode)) {
        header("Location: ../sign_up.php?error=emptyfields&uid=" . $username . "&mail=" . $email);
        exit();
    } elseif (strlen($username) < 3) {
        header("Location: ../sign_up.php?error=short_username=" . $username . "");
        exit();
    } elseif (strlen($username) > 17) {
        header("Location: ../sign_up.php?error=long_username=" . $username . "");
        exit();
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL) && !preg_match("/^[a-zA-Z0-9]*$/", $username)) {
        header("Location: ../sign_up.php?error=invalidmailuid");
        exit();
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        header("Location: ../sign_up.php?error=invalidmail&uid=" . $username);
        exit();
    } elseif (!preg_match("/^[a-zA-Z0-9]*$/", $username)) {
        header("Location: ../sign_up.php?error=invaliduid&mail=" . $email);
        exit();
    } elseif ($password !== $passwordRepeat) {
        header("Location: ../sign_up.php?error=passwordcheck&uid=" . $username . "&mail=" . $email);
        exit();
    } else {
        $sql = "SELECT username FROM users WHERE username=?";
        $stmt = mysqli_stmt_init($conn);
        if (!mysqli_stmt_prepare($stmt, $sql)) {
            header("Location: ../sign_up.php?error=sqlerror");
            exit();
        } else {
            mysqli_stmt_bind_param($stmt, "s", $username);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_store_result($stmt);
            $resultCheck = mysqli_stmt_num_rows($stmt);
            if ($resultCheck > 0) {
                header("Location: ../sign_up.php?error=usertaken&mail=" . $email);
                exit();
            } else {
                $sql = "INSERT INTO users (username, email, password) VALUES (?, ?, ?)";
                $stmt = mysqli_stmt_init($conn);
                if (!mysqli_stmt_prepare($stmt, $sql)) {
                    header("Location: ../sign_up.php?error=sqlerror");
                    exit();
                } else {
                    $hashedPwd = password_hash($password, PASSWORD_DEFAULT);

                    mysqli_stmt_bind_param($stmt, "sss", $username, $email, $hashedPwd);
                    mysqli_stmt_execute($stmt);

                    $sql = "INSERT INTO security (username, firstName, lastName, country, city, address, zipcode) VALUES (?, ?, ?, ?, ?, ?, ?)";
                    $stmt = mysqli_stmt_init($conn);
                    mysqli_stmt_prepare($stmt, $sql);
                    mysqli_stmt_bind_param($stmt, "sssssss", $username, $firstName, $lastName, $country, $city, $address, $zipCode);
                    mysqli_stmt_execute($stmt);

                    header("Location: ../sign_up.php?signup=succes");
                    exit();
                }
            }
        }
    }
    mysqli_stmt_close($stmt);
    mysqli_close($conn);
} else {
    header("Location: ../sign_up.php");
    exit();
}

Replace username from security with id from "users" table, but I don't know the id from this user because it executes at the same time, any solutions?

P.S: ID, auto increment primary key

I want to know that security data is from that user (id).

Upvotes: 1

Views: 39

Answers (1)

Thanasis Balatsoykas
Thanasis Balatsoykas

Reputation: 176

You can get the last inserted id in mysqli using

$conn->insert_id;

Right after executing the insertion of the item. ($conn being the instance of the msqli class)

Upvotes: 1

Related Questions