-1

I want to use the INSERT statement to insert values that come from the SELECT statements in PHP

the table of student is not get the data

$sql = "INSERT INTO student(academic_major, promo, user_id) VALUES (?, ?, (?));";
$stmtt = mysqli_stmt_init($conn);
if(!mysqli_stmt_prepare($stmtt, $sql)){
    header("Location: add_student_forum.php?error=sqlerrorstudent");
    exit();
}else{

    $id = "SELECT id FROM user WHERE email = '$email'";
    mysqli_stmt_bind_param($stmtt, "sss", $academic_major, $promo, $id);
    mysqli_stmt_execute($stmtt);
    header("Location: add_student_forum.php?signup=success".$id);
    exit();
}

when i execute it shows me this header header("Location: add_student_forum.php?signup=success".$id); in the url and i dont know why the table is empty after

Akina
  • 39,301
  • 5
  • 14
  • 25
haithemB
  • 259
  • 1
  • 2
  • 6

1 Answers1

1

You don't bind SQL as a parameter. Bind the data as parameter and put the SELECT SQL in the prepared statement SQL

$sql = "INSERT INTO student(academic_major, promo, user_id) VALUES (?, ?, SELECT id FROM user WHERE email = ?);";
$stmtt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmtt, $sql);
mysqli_stmt_bind_param($stmtt, "sss", $academic_major, $promo, $email);
mysqli_stmt_execute($stmtt);
header("Location: add_student_forum.php?signup=success".$id);
exit();

Make sure you have mysqli error reporting enabled. How to get the error message in MySQLi?

Dharman
  • 30,962
  • 25
  • 85
  • 135