1

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).

Dharman
  • 30,962
  • 25
  • 85
  • 135
MoonDarius
  • 61
  • 5
  • _“because it executes at the same time”_ - no, it doesn’t, these INSERT statements execute one after the other, not somehow “in parallel”. – 04FS Oct 02 '20 at 12:52
  • 2
    https://www.php.net/manual/en/mysqli.insert-id.php – 04FS Oct 02 '20 at 12:52
  • You need to stop manually checking for errors. Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) and [Should I manually check for errors when calling “mysqli_stmt_prepare”?](https://stackoverflow.com/q/62216426/1839439) – Dharman Oct 02 '20 at 13:12
  • 1
    Not sure what you mean but you can get the autoincrement ID from the previous query and use it in the next one – Dharman Oct 02 '20 at 13:12
  • Yes dharman, that s what i want – MoonDarius Oct 02 '20 at 13:54

1 Answers1

1

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)