0

Trying to query the database after form submission for "UID", then inserting it in proper table based on a certain value of the submitted form. I am getting an error on this line
$stmt->bind_param("i", $uid);
Why? It says
Call to a member function bind_param() on bool
Anyone understand this?

$stmt = $conn->prepare("INSERT INTO users (type,username,password) VALUES (?,?,?)");
$stmt->bind_param("sss", $_POST['acct-reg'],$_POST['username-reg'],$_POST['pwd-reg']);
if ($stmt->execute()){
    $sql= "SELECT uid FROM users WHERE username = ?";
    $stmt = $conn->prepare($sql);
    $stmt->bind_param("s", $_POST['username-reg']);
    $stmt->execute();
    $stmt->bind_result($uid);
    while($stmt->fetch()) {
        if ($_POST['acct-reg'] == 'a'){
            $sql="INSERT into tasker (uid) VALUES (?)";
            $stmt= $conn->prepare($sql);
            $stmt->bind_param("i", $uid);
            $stmt->execute();
            $stmt->close();
}
}
};
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Because `prepare()` is getting an error and returning `FALSE`. – Barmar Mar 03 '20 at 01:46
  • why is the prepare statement getting an error? im following exactly as before, that statement did not get an error –  Mar 03 '20 at 01:54
  • What does `echo $conn->error;` say? – Barmar Mar 03 '20 at 01:58
  • Uncaught mysqli_sql_exception: Commands out of sync; you can't run this command now... mysqli->prepare('INSERT into tas...') –  Mar 03 '20 at 02:07
  • See https://stackoverflow.com/questions/614671/commands-out-of-sync-you-cant-run-this-command-now – Barmar Mar 03 '20 at 02:31
  • Are you just trying to get the auto increment UID that was assigned to the new user? Use `INSERT INTO tasker(uid) VALUES (LAST_INSERT_ID())`. You don't need the `SELECT` query. – Barmar Mar 03 '20 at 02:33
  • So quite literally? $sql="INSERT into tasker (uid) VALUES (LAST_INSERT_ID())"; It knows to get the last inserted ID from the users table? –  Mar 03 '20 at 04:20
  • Yes, `LAST_INSERT_ID()` is the last auto increment ID that was created on the MySQL connection. – Barmar Mar 03 '20 at 14:47

0 Answers0