0

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);
2
  • 4
    put the last line inside the if Commented Nov 21, 2017 at 20:55
  • 1
    Note: The object-oriented interface to mysqli is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete mysql_query interface. Before you get too invested in the procedural style it’s worth switching over. Example: $db = new mysqli(…) and $db->prepare("…") The procedural interface is an artifact from the PHP 4 era when mysqli API was introduced and should not be used in new code. Commented Nov 21, 2017 at 21:20

4 Answers 4

2
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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

No, replacing the '?'s with 'null' did not fix the error
0

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

Comments

-1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.