简体   繁体   中英

How to insert id from another table to a table using one form?

I have to get the user_id from the users table and insert it to the user_id of patients table using only one form.

This is the tables I am using: user_table : user_id is auto increment.

╔═════════╦══════════╦════════════════╦═══════════╗ 
║ user_id ║ level_id ║  email         ║  password ║ 
╠═════════╬══════════╬════════════════╬═══════════╣ 
║ 1       ║  5       ║ sasa@denva.com ║ sasadenva ║ 
║ 2       ║  1       ║ tony@stark.com ║ tonystark ║ 
╚═════════╩══════════╩════════════════╩═══════════╝

patients_table: patients_id is auto increment.

+--------+---------+--------------+----------+-----------+--------+
| pat_id | user_id | name         | address  | number    | sex    |
| 1      | 1       | sasa mindred | manhatan | 987654329 | female |
| 2      | 2       | tony stark   | new york | 123456789 | male   |
+--------+---------+--------------+----------+-----------+--------+

I made the name short for the table here.

$sql = "SELECT email FROM users WHERE email=?";
        $stmt =mysqli_stmt_init($conn);
        if(!mysqli_stmt_prepare($stmt, $sql)) {
        header("Location: ../auth/register.php?error=sqlerror");
        exit();

        }else{
            mysqli_stmt_bind_param($stmt, "s", $email);
            mysqli_stmt_execute($stmt);
            mysqli_stmt_store_result($stmt);
            $resultcheck= mysqli_stmt_num_rows($stmt);
            if($resultcheck > 0) {
                header("Location: ../auth/register.php?error=emailtaken&last_name=".$last."&first_name=".$first."&middle_name=".$middle."&sex=".$sex."");
                exit();

            }else{
                $sql= "INSERT INTO users (level_id, email, password, created_at) VALUES (?, ?, ?, now())";
                $stmt = mysqli_stmt_init($conn);

                if(!mysqli_stmt_prepare($stmt, $sql)){
                    header("Location: ../auth/register.php?error=sqlerror");
                    exit();

                }else{
                    $hashedPwd = password_hash($password, PASSWORD_DEFAULT);
                    mysqli_stmt_bind_param($stmt, "iss", $lvl, $email, $hashedPwd);

                        $user_id = mysqli_insert_id($conn);
                        $sqli = "INSERT INTO patients (user_id, last_name, first_name, middle_name, sex) VALUES ($user_id, ?, ?, ?, ?)";

                        $stmt = mysqli_stmt_init($conn);
                        if(!mysqli_stmt_prepare($stmt, $sql)){
                            header("Location: ../auth/register.php?error=sqlerror");
                            exit();

                        }else{
                            mysqli_stmt_bind_param($stmt, "ssss", $last, $first, $middle, $sex);
                            mysqli_stmt_execute($stmt);
                        }


                    mysqli_stmt_execute($stmt);
                    header("Location: ../auth/register.php?signup=success".$conn->insert_id."");
                    exit();

                }


            }
        }

I expected the output to insert the user_id from the users table to the user_id of the patients table.

This is the solved codes if anyone like to know.

else{

    $sql = "SELECT email FROM users WHERE email=?";
    $stmt =mysqli_stmt_init($conn);
    if(!mysqli_stmt_prepare($stmt, $sql)) {
    header("Location: ../auth/register.php?error=sqlerror");
    exit();

    }else{
        mysqli_stmt_bind_param($stmt, "s", $email);
        mysqli_stmt_execute($stmt);
        mysqli_stmt_store_result($stmt);
        $resultcheck= mysqli_stmt_num_rows($stmt);
        if($resultcheck > 0) {
            header("Location: ../auth/register.php?error=emailtaken&last_name=".$last."&first_name=".$first."&middle_name=".$middle."&sex=".$sex."");
            exit();

        }else{
            $sql= "INSERT INTO users (level_id, email, password, created_at) VALUES (?, ?, ?, now())";
            $stmt = mysqli_stmt_init($conn);

            if(!mysqli_stmt_prepare($stmt, $sql)){
                header("Location: ../auth/register.php?error=sqlerror");
                exit();

            }else{
                $hashedPwd = password_hash($password, PASSWORD_DEFAULT);
                mysqli_stmt_bind_param($stmt, "iss", $lvl, $email, $hashedPwd);

                mysqli_stmt_execute($stmt);

                    $user_id = mysqli_insert_id($conn);
                    $sqli = "INSERT INTO patients (user_id, last_name, first_name, middle_name, sex) VALUES ($user_id, ?, ?, ?, ?)";

                    $stmt = mysqli_stmt_init($conn);
                    if(!mysqli_stmt_prepare($stmt, $sqli)){
                        header("Location: ../auth/register.php?error=sqlerror");
                        exit();

                    }else{
                        mysqli_stmt_bind_param($stmt, "ssss", $last, $first, $middle, $sex);
                    }


                mysqli_stmt_execute($stmt);
                header("Location: ../auth/register.php?signup=success".$conn->insert_id."");
                exit();

            }

You are trying to get the inserted id before you insert (execute) it.

First, You need to execute it:

mysqli_stmt_execute($stmt);

By adding it before:

$user_id = mysqli_insert_id($conn);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM