Home:ALL Converter>Boolean PHP MySQL Parameter Error

Boolean PHP MySQL Parameter Error

Ask Time:2017-11-22T04:54:12         Author:Jared

Json Formatter

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);

Author:Jared,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/47422533/boolean-php-mysql-parameter-error
Hugo Delsing :

if($stmt = mysqli_prepare($link, $sql)){\n ...\n}\n\n\nThis is the same as \n\n$stmt = mysqli_prepare($link, $sql);\nif($stmt){\n ...\n}\n\n\nSo 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.\n\nif($stmt = mysqli_prepare($link, $sql)){\n ...\n mysqli_stmt_close($stmt);\n}\n\n\nNow 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.\n\n$link = mysqli_connect(\"localhost\", \"user\", \"password\", \"db\");\n\n/* check connection */\nif (mysqli_connect_errno()) {\n printf(\"Connect failed: %s\\n\", mysqli_connect_error());\n exit();\n}\n\n\nif($stmt = mysqli_prepare($link, $sql)){\n ...\n mysqli_stmt_close($stmt);\n}\n",
2017-11-21T21:12:45
Alexandre Sauvé :

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.",
2017-11-21T21:02:46
zstate :

Use this code to check last statement error:\n\nif(mysqli_stmt_execute($stmt)){\n // Redirect to login page\n header(\"location: index.php\");\n} else{\n printf(\"Error: %s.\\n\", mysqli_stmt_error($stmt));\n echo \"Something went wrong. Please try again later.\";\n}\n\n\nhttp://php.net/manual/en/mysqli-stmt.error.php",
2017-11-21T21:30:17
yy