我必须从users表中获取user_id
并将其插入到患者表的user_id
中,而仅使用一种形式。
这是我正在使用的表:
user_table
:user_id
是自动递增。
╔═════════╦══════════╦════════════════╦═══════════╗
║ user_id ║ level_id ║ email ║ password ║
╠═════════╬══════════╬════════════════╬═══════════╣
║ 1 ║ 5 ║ sasa@denva.com ║ sasadenva ║
║ 2 ║ 1 ║ tony@stark.com ║ tonystark ║
╚═════════╩══════════╩════════════════╩═══════════╝
Patients_table:Patient_id是自动递增。
+--------+---------+--------------+----------+-----------+--------+
| pat_id | user_id | name | address | number | sex |
| 1 | 1 | sasa mindred | manhatan | 987654329 | female |
| 2 | 2 | tony stark | new york | 123456789 | male |
+--------+---------+--------------+----------+-----------+--------+
我在这里把桌子的名字简称了。
$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();
}
}
}
我希望输出将用户表中的user_id插入患者表的user_id中。
如果有人愿意,这是已解决的代码。
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();
}
答案 0 :(得分:0)
您正在尝试获取插入的ID,然后再插入(执行)它。
首先,您需要执行它:
mysqli_stmt_execute($stmt);
通过在之前添加它:
$user_id = mysqli_insert_id($conn);