doubi6669 2017-11-21 20:54
浏览 36
已采纳

布尔PHP MySQL参数错误

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);
  • 写回答

4条回答 默认 最新

  • dongquming3255 2017-11-21 21:12
    关注
    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
    ", mysqli_connect_error());
      exit();
    }
    
    
    if($stmt = mysqli_prepare($link, $sql)){
      ...
      mysqli_stmt_close($stmt);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 深度学习残差模块模型
  • ¥20 两个不同Subnet的点对点连接
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用
  • ¥15 请帮我看一看数电项目如何设计
  • ¥23 (标签-bug|关键词-密码错误加密)