"mysqli_stmt object is not fully initialized" error message

Viewed 2504

I'm trying to make a registeration form using php and sql but i keep getting "mysqli_stmt object is not fully initialized" as an error message.
Registration.php file:

$sql = "INSERT INTO user_table ('user_name', 'user_email', 'user_password') VALUES (?, ?, ?)";
$stmt = mysqli_stmt_init($conn);
if (mysqli_stmt_prepare($stmt, $sql)){
    header("location: ../signup.php?error=sqlerror");
    exit();
} else {
    //hashing password
    $hashedPass = password_hash($password, PASSWORD_DEFAULT);
    //inserting hashed password.
    mysqli_stmt_bind_param($stmt, "sss", $username, $email, $hashedPass);
    mysqli_stmt_execute($stmt);
    
    header("location: ../signup.php?success=registered");
    exit();
}

I think the problem is in this part (in the line number 8).

1 Answers

If we remove your if/else check and run everything, with the mysqli_stmt_prepare after the mysqli_stmt_init we get what we expect.

$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt, $sql);

So the logic on your if seems like it needs a ! (meaning false). Try this alteration to your code block:

$sql = 'INSERT INTO user_table (user_name, user_email, user_password) VALUES (?, ?, ?)';
$stmt = mysqli_stmt_init($conn);

if (!mysqli_stmt_prepare($stmt, $sql)) {
    header("location: ../signup.php?error=sqlerror");
    exit();
} else {
    //hashing password
    $hashedPass = password_hash($password, PASSWORD_DEFAULT);

    //inserting hashed password.
    mysqli_stmt_bind_param($stmt, 'sss', $username, $email, $hashedPass);
    mysqli_stmt_execute($stmt);

    header("location: ../signup.php?success=registered");
    exit();
}

The above code worked in my test.

Related