如何修复mysqli_stmt_close()期望参数1为mysqli_stmt错误

时间:2019-07-18 05:00:24

标签: php

我有一个用于注册用户的页面,该页面将数据存储到名为车库的数据库中。最初,该表格运行良好,可以将数据保存到数据库中,但是现在我一直遇到错误。表单将以下字段存储到表admin_users中(名字,姓氏,id号,电子邮件,地址,电话号码,镇,adminuname,adminpass)。

这里有一些代码用于将数据存储到数据库中。

<?php
// Initialize the session
session_start();
// Check if the user is logged in, if not then redirect him to login page
// if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
//     header("location: index.php");
//     exit;
// }

// registration
require 'includes/connect.php';
// Define variables and initialize with empty values
$a_fname = $a_lname = $a_idno = $a_email = $a_address = $a_tel = $a_town = $a_uname = $a_pass = $a_cpass = "";
$form_err = $form_msg = $a_fname_err = $a_lname_err = $a_idno_err = $a_email_err = $a_address_err = $a_tel_err = $a_town_err = $a_uname_err = $a_pass_err = $a_cpass_err = "";

// Processing form data when form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {

    //validate firstname
    if (empty(trim($_POST["admin_fname"]))) {
        $a_fname_err = "Please enter your firstname.";
    }
    elseif(strlen(trim($_POST["admin_fname"])) > 15) {
        $a_fname_err = "Firstname cannot exceed 15 characters.";
    } else {
        $a_fname = trim($_POST["admin_fname"]);
    }

    //validate lastname
    if (empty(trim($_POST["admin_lname"]))) {
        $a_lname_err = "Please enter your lastname.";
    }
    elseif(strlen(trim($_POST["admin_lname"])) > 15) {
        $a_lname_err = "Lastname cannot exceed 15 characters.";
    } else {
        $a_lname = trim($_POST["admin_lname"]);
    }

    //validate firstname
    if (empty(trim($_POST["admin_idno"]))) {
        $a_idno_err = "Please enter your Idnumber.";
    }
    elseif(strlen(trim($_POST["admin_idno"])) > 10) {
        $a_idno_err = "Idnumber cannot exceed 10 characters.";
    } else {
        $a_idno = trim($_POST["admin_idno"]);
    }

    //validate email
    if (empty(trim($_POST["admin_email"]))) {
        $a_email_err = "Please enter your email address.";
    }elseif(filter_var($_POST["admin_email"], FILTER_VALIDATE_EMAIL)) {
        $a_email = trim($_POST["admin_email"]);
    } else {
        $a_email_err = "Email address is invalid.";
    }

    //validate address
    if (empty(trim($_POST["admin_address"]))) {
        $a_address_err = "Please enter your address.";
    } else {
        $a_address = trim($_POST["admin_address"]);
    }

    //Validate phone Number
    if (empty(trim($_POST["admin_tel"]))) {
        $a_tel_err = "Please enter Mobile number";
    } elseif(preg_match('/^[0-9]{10}+$/', $_POST["admin_tel"])) {
      $a_tel = trim($_POST["admin_tel"]);
      }else{
        $a_tel_err = "Invalid Telephone Number";
      }

    //validate town
    if (empty(trim($_POST["admin_town"]))) {
        $a_town_err = "Please enter your town.";
    } else {
        $a_town = trim($_POST["admin_town"]);
    }

    // Validate admin_uname
    if (empty(trim($_POST["admin_uname"]))) {
        $a_uname_err = "Please enter your username.";
    } else {
        // Prepare a select statement
        $sql = "SELECT id FROM admin_users WHERE adminuname = ?";

        if ($stmt = mysqli_prepare($mysqli, $sql)) {
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "s", $param_admin_uname);

            // Set parameters
            $param_admin_uname = trim($_POST["admin_uname"]);

            // Attempt to execute the prepared statement
            if (mysqli_stmt_execute($stmt)) {
                /* store result */
                mysqli_stmt_store_result($stmt);

                if (mysqli_stmt_num_rows($stmt) == 1) {
                    $a_uname_err = "This username is already taken.";
                } else {
                    $a_uname = trim($_POST["admin_uname"]);
                }
            } else {
                $form_err = "Oops! Something went wrong. Please try again later.";
            }
        }

        // Close statement
        mysqli_stmt_close($stmt);
    }

    // Validate password
    if (empty(trim($_POST["admin_pass"]))) {
        $a_pass_err = "Please enter your password.";
    }
    elseif(strlen(trim($_POST["admin_pass"])) < 6) {
        $a_pass_err = "Password must have atleast 6 characters.";
    } else {
        $a_pass = trim($_POST["admin_pass"]);
    }

    // Validate confirm password
    if (empty(trim($_POST["admin_cpass"]))) {
        $a_cpass_err = "Please confirm password.";
    } else {
        $a_cpass = trim($_POST["admin_cpass"]);
        if (empty($a_pass_err) && ($a_pass != $a_cpass)) {
            $a_cpass_err = "Password did not match.";
        }
    }

    // Check input errors before inserting in database
    if (empty($a_fname_err) && empty($a_lname_err) && empty($a_idno_err) && empty($a_email_err) && empty($a_address_err) && empty($a_tel_err) && empty($a_town_err) && empty($a_uname_err) && empty($a_pass_err) && empty($a_cpass_err)) {

        // Prepare an insert statement
        $sql = "INSERT INTO admin_users (firstname, lastname, idnumber, email,  address,  telnumber,  town, adminuname, adminpass) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";

        if ($stmt = mysqli_prepare($mysqli, $sql)) {
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "sssssssss", $param_fname, $param_lname, $param_idno, $param_email, $param_address, $param_tel, $param_town, $param_admin_uname, $param_password);

            // Set parameters
            $param_fname = $a_fname;
            $param_lname = $a_lname;
            $param_idno = $a_idno;
            $param_email = $a_email;
            $param_address = $a_address;
            $param_tel = $a_tel;
            $param_town = $a_town;
            $param_admin_uname = $a_uname;
            $param_password = password_hash($a_pass, PASSWORD_DEFAULT); // Creates a password hash

            // Attempt to execute the prepared statement
            if (mysqli_stmt_execute($stmt)) {
                $form_msg = "User registered successfully!";
                $a_fname = $a_lname = $a_idno = $a_email = $a_address = $a_tel = $a_town = $a_uname = $a_pass = $a_cpass = "";
            } else {
                $form_err = "Something went wrong. Please try again later.";
            }
        }

        // Close statement
        mysqli_stmt_close($stmt);
    } else {
      $form_err = "Something went wrong. Please try again later." . mysqli_error($mysqli);
    }

    // Close connection
    mysqli_close($mysqli);

}
?>

这是我现在遇到的错误。

  

警告:mysqli_stmt_close()期望参数1为mysqli_stmt,在第111行的C:\ xampp \ htdocs \ garage \ admin \ add_admin.php中给出的布尔值

     

警告:mysqli_stmt_close()期望参数1为mysqli_stmt,在第165行的C:\ xampp \ htdocs \ garage \ admin \ add_admin.php中给出布尔值   请协助。

0 个答案:

没有答案