Jared
Jared

Reputation: 471

Boolean PHP MySQL Parameter Error

Alright, I have been working at this for some time. Cannot seem to locate the issue. I have searched through Stackoverflow for similar issues but they all appear to point at the prepare statement for typos.

I haven't found any typos here but I am still getting the error

Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, boolean given

$sql = "INSERT INTO users (email, password, firstname, lastname, date, position, department, manager, birthdate) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";

if($stmt = mysqli_prepare($link, $sql)){
    mysqli_stmt_bind_param($stmt, "sssssssss", $param_email, $param_password, $param_firstname, $param_lastname, $param_department, $param_position, $param_manager, $param_date, $param_birthdate);

    $param_email = $email;
    $param_password = password_hash($password, PASSWORD_DEFAULT);
    $param_firstname = $firstname;
    $param_lastname = $lastname;
    $param_department = $department;
    $param_position = $position;
    $param_manager = $manager;
    $param_date = $date;
    $param_birthdate = $birthdate;

    if(mysqli_stmt_execute($stmt)){
        // Redirect to login page
        header("location: index.php");
    } else{
        echo "Something went wrong. Please try again later.";
    }
}

mysqli_stmt_close($stmt);

Upvotes: 0

Views: 247

Answers (4)

zstate
zstate

Reputation: 2055

Use this code to check last statement error:

if(mysqli_stmt_execute($stmt)){
    // Redirect to login page
    header("location: index.php");
} else{
    printf("Error: %s.\n", mysqli_stmt_error($stmt));
    echo "Something went wrong. Please try again later.";
}

http://php.net/manual/en/mysqli-stmt.error.php

Upvotes: 0

Hugo Delsing
Hugo Delsing

Reputation: 14163

if($stmt = mysqli_prepare($link, $sql)){
  ...
}

This is the same as

$stmt = mysqli_prepare($link, $sql);
if($stmt){
  ...
}

So in your case, the failure on the last line means that the result of mysqli_prepare($link, $sql) is a (boolean) false. Since you have the last line outside the if, it will try to close a false instead of a prepared query. Putting it inside the if statement, will solve your error, but not why the result is false.

if($stmt = mysqli_prepare($link, $sql)){
  ...
  mysqli_stmt_close($stmt);
}

Now for some reason mysqli_prepare fails. This can be because of the $link or because of the $sql. The $sql is given and seems to be alright, so the $link must be failing.

$link = mysqli_connect("localhost", "user", "password", "db");

/* check connection */
if (mysqli_connect_errno()) {
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
}


if($stmt = mysqli_prepare($link, $sql)){
  ...
  mysqli_stmt_close($stmt);
}

Upvotes: 2

Ai Da
Ai Da

Reputation: 17

variable $stmt is declared to have the type mysqli_stmt within the if($stmt = mysqli_prepare($link, $sql)){} statement. declare $stmt before the if-statement or move the last line inside the if-statement.

Upvotes: -1

Pøziel
Pøziel

Reputation: 199

In 70% of the case, the problem is in your query. The reason why Mysql is giving you an error is because this ($stmt = mysqli_prepare($link, $sql) have an error and the mysqli_prepare method is returning a bool. It should be your query with your ? as a value. Try replace them with null. Tell me if it work.

Upvotes: 0

Related Questions