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

问题描述 投票:0回答:1

我在网站上工作时遇到参数错误。我不知道如何绕过,以及如何将数组添加到此代码中的复选框。我收到以下错误代码

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

警告:trim()期望参数1为字符串,数组位于C:\ xampp \ htdocs \ formnew.php,第55行

我的代码如下

<?php
// Include config file
require_once "config.php";

// Define variables and initialize with empty values
$name = $id = $checkbox = "";
$name_err = $id_err = $checkbox_err = "";

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

// Validate username
    if(empty(trim($_POST["name"]))){
        $username_err = "Please enter your name.";
    } else{
        // Prepare a select statement
        $sql = "SELECT FROM form WHERE name = ?";

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

            // Set parameters
            $param_name = trim($_POST["name"]);

            // 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){
                    $name_err = "you've already submitted a request.";
                } else{
                    $name = trim($_POST["name"]);
                }
            } else{
                echo "Oops! Something went wrong. Please try again later.";
            }
        }

        // Close statement
        mysqli_stmt_close($stmt);
    }

    // Validate id
    if(empty(trim($_POST["id"]))){
        $password_err = "Please enter your ID.";     
    } elseif(strlen(trim($_POST["id"])) < 7){
        $id_err = "Please enter your full student ID.";
    } else{
        $id = trim($_POST["id"]);
    }

    // checkbox
    if(empty(trim($_POST["checkbox"]))){
        $checkbox_err = "Please select trips.";     
    } else{
        $checkbox = trim($_POST["checkbox"]);
    }

    // Check input errors before inserting in database
    if(empty($name_err) && empty($id_err) && empty($checkbox_err)){

        // Prepare an insert statement
        $sql = "INSERT INTO users (name, id, checkbox) VALUES (?, ?, ?)";

        if($stmt = mysqli_prepare($link, $sql)){
            // Bind variables to the prepared statement as parameters
            mysqli_stmt_bind_param($stmt, "sss", $param_name, $param_id, $param_checkbox);

            // Set parameters
            $param_name = $name;
            $param_id = $id;
        $param_checkbox = $checkbox;

            // Attempt to execute the prepared statement
            if(mysqli_stmt_execute($stmt)){
                // Redirect to login page
                header("location: form.php");
            } else{
                echo "Something went wrong. Please try again later.";
            }
        }

        // Close statement
        mysqli_stmt_close($stmt);
    }

    // Close connection
    mysqli_close($link);
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Sign Up</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
    <style type="text/css">
        body{ font: 14px sans-serif; }
        .wrapper{ width: 350px; padding: 20px; }
    </style>
</head>
<body>
    <div class="wrapper">
        <h2>Sign Up</h2>
        <p>Please fill this form to create an account.</p>
        <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
            <div class="form-group <?php echo (!empty($name_err)) ? 'has-error' : ''; ?>">
                <label>Name</label>
                <input type="text" name="name" class="form-control" value="<?php echo $name; ?>">
                <span class="help-block"><?php echo $name_err; ?></span>
            </div>    
            <div class="form-group <?php echo (!empty($id_err)) ? 'has-error' : ''; ?>">
                <label>Student ID</label>
                <input type="text" name="id" class="form-control" value="<?php echo $id; ?>">
                <span class="help-block"><?php echo $id_err; ?></span>
            </div>
          <div class="form-group <?php echo (!empty($checkbox_err)) ? 'has-error' : ''; ?>">
                <label>Trips</label>
                <input type="checkbox" name="checkbox[]" class="form-control" value="<?php echo $test1; ?>">
        <input type="checkbox" name="checkbox[]" class="form-control" value="<?php echo $test2; ?>">
        <input type="checkbox" name="checkbox[]" class="form-control" value="<?php echo $test3; ?>">
        <input type="checkbox" name="checkbox[]" class="form-control" value="<?php echo $test4; ?>">
        <input type="checkbox" name="checkbox[]" class="form-control" value="<?php echo $test5; ?>">
                <span class="help-block"><?php echo $checkbox_err; ?></span>
            </div>
            <div class="form-group">
                <input type="submit" class="btn btn-primary" value="Submit">
                <input type="reset" class="btn btn-default" value="Reset">
            </div>
        </form>
    </div>    
</body>
</html>

并且我的sql表设置为

Name - ID - Checkbox with the parameters being varchar(50)
php mysqli prepared-statement
1个回答
0
投票

mysqli_stmt_close()放入if块中,因此仅当mysqli_prepare()成功时才执行。

并且如果mysqli调用失败,您应该显示或记录错误消息,而不仅仅是“糟糕”。

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

            // Set parameters
            $param_name = trim($_POST["name"]);

            // 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){
                    $name_err = "you've already submitted a request.";
                } else{
                    $name = trim($_POST["name"]);
                }
            } else{
                echo "Execute query error: " . mysqli_stmt_error($stmt);
            }
            // Close statement
            mysqli_stmt_close($stmt);
        } else {
            echo "Prepare statement error: " . mysqli_error($link);
        }

© www.soinside.com 2019 - 2024. All rights reserved.