mysqli_stmt_close()期望为参数1

时间:2018-03-03 09:02:13

标签: php mysql

我已在PHP进行编码并将其连接到MySql。我一直在收到错误

  

警告:mysqli_stmt_close()期望参数1为mysqli_stmt,第175行的C:\ xampp \ htdocs \ xxxx中给出布尔值

这段代码有什么问题吗?

 if(empty(trim($_POST['dept'])) && empty(trim($_POST['fname'])) && empty(trim($_POST['lname'])) && empty(trim($_POST['email'])) && empty(trim($_POST['cnum'])) && empty(trim($_POST['dob'])) && empty(trim($_POST['gender'])) && empty(trim($_POST['minitial'])) && strlen(trim($_POST['minitial'])) > 1   ){
            $regerr = "Please complete your information";
        }else{
            $fname = trim($_POST['fname']);
            $lname = trim($_POST['lname']);
            $email = trim($_POST['email']);
            $cnum = trim($_POST['cnum']);
            $dob = trim($_POST['dob']);
            $gender = trim($_POST['gender']);
            $dept = trim($_POST['dept']);
            $minitial = trim($_POST['minitial']);
        }

        // Check input errors before inserting in database
        if(empty($usernameerr) && empty($passworderr) && empty($confirmpassworderr) && empty($regerr) ){

            // Prepare an insert statement
            $sql = "INSERT INTO login_info (username, password) VALUES (?, ?);
                    INSERT INTO user_info (fname, sname, minitial, contact_num, gender, dob, department) VALUES (?,?,?,?,?,CAST (? AS DATE),?);";

            if($stmt = mysqli_prepare($link, $sql)){
                // Bind variables to the prepared statement as parameters
                mysqli_stmt_bind_param($stmt, "sssssssss", $param_username, $param_password, $param_fname, $param_sname, $param_minitial, $param_cnum, $param_gender, $param_dob, $param_department);

                // Set parameters
                $param_username = $username;
                $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
                $param_fname = $fname;
                $param_sname = $lname;
                $param_minitial =$minitial;
                $param_cnum = $cnum;
                $param_gender = $gender;
                $param_dob = $dob;
                $param_department = $dept;

                // Attempt to execute the prepared statement
                if(mysqli_stmt_execute($stmt)){

                    session_start();
                    $_SESSION['username'] = $username;
                    $_SESSION['usernumber'] = $mysqli_insert_id($link);
                    header("location: home-trabawho.php");
                } else{
                    echo "Something went wrong. Please try again later.";
                }
            }

            // Close statement
            mysqli_stmt_close($stmt);
        }

1 个答案:

答案 0 :(得分:1)

问题在于:

        // Prepare an insert statement
        $sql = "INSERT INTO login_info (username, password) VALUES (?, ?);
                INSERT INTO user_info (fname, sname, minitial, contact_num, gender, dob, department) VALUES (?,?,?,?,?,CAST (? AS DATE),?);";

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

没有一个插入语句,因为评论假装,有两个语句。

mysqli_prepare()的文档清楚地解释了:

 mysqli_stmt mysqli_prepare ( mysqli $link , string $query )
     

准备SQL查询,并返回一个语句句柄,用于对语句进行进一步操作。 查询必须包含单个SQL语句。

因此,$stmtfalseif分支未执行且

        mysqli_stmt_close($stmt);

正确失败。

首先,您应该将mysqli_stmt_close($stmt)块内的呼叫置于if块内。您应该检查mysqli_prepare()返回的值并相应地处理错误。

在这种特定情况下,您应该使用两对mysqli_prepare() / mysqli_stmt_execute()语句来运行两个INSERT查询。在user_info表中,您可能还需要插入第一个INSERT查询生成的ID,以链接这些查询插入的两个记录。