php - 错误:mysqli_stmt::bind_param():类型定义字符串中的元素数量与绑定(bind)变量的数量不匹配

标签 php mysqli

<分区>

我不确定哪里出了问题,一切似乎都很好。

Warning: mysqli_stmt::bind_param(): Number of elements in type definition string doesn't match number of bind variables on line 88

<?php
error_reporting(E_ALL); 
ini_set('display_errors', 1);

include_once 'db_connect.php';
include_once 'ex-config.php';



$error_msg = "";

if (isset($_POST['username'], $_POST['email'], $_POST['p'])) {
    // Sanitize and validate the data passed in
    $username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
    $email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
    $email = filter_var($email, FILTER_VALIDATE_EMAIL);
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        // Not a valid email
        $error_msg .= '<p class="error">The email address you entered is not valid</p>';
    }

    $password = filter_input(INPUT_POST, 'p', FILTER_SANITIZE_STRING);
    if (strlen($password) != 128) {
        // The hashed pwd should be 128 characters long.
        // If it's not, something really odd has happened
        $error_msg .= '<p class="error">Invalid password configuration.</p>';
    }

    // Username validity and password validity have been checked client side.
    // This should should be adequate as nobody gains any advantage from
    // breaking these rules.
    //

    $prep_stmt = "SELECT id FROM members WHERE email = ? LIMIT 1";
    $stmt = $mysqli->prepare($prep_stmt);

   // check existing email  
    if ($stmt) {
        $stmt->bind_param('s', $email);
        $stmt->execute();
        $stmt->store_result();

        if ($stmt->num_rows == 1) {
            // A user with this email address already exists
            $error_msg .= '<p class="error">A user with this email address already exists.</p>';
                        $stmt->close();
        }
                $stmt->close();
    } else {
        $error_msg .= '<p class="error">Database error Line 39</p>';
                $stmt->close();
    }

    // check existing username
    $prep_stmt = "SELECT id FROM members WHERE username = ? LIMIT 1";
    $stmt = $mysqli->prepare($prep_stmt);

    if ($stmt) {
        $stmt->bind_param('s', $username);
        $stmt->execute();
        $stmt->store_result();

                if ($stmt->num_rows == 1) {
                        // A user with this username already exists
                        $error_msg .= '<p class="error">A user with this username already exists</p>';
                        $stmt->close();
                }
                $stmt->close();
        } else {
                $error_msg .= '<p class="error">Database error line 55</p>';
                $stmt->close();
        }

    // TODO: 
    // We'll also have to account for the situation where the user doesn't have
    // rights to do registration, by checking what type of user is attempting to
    // perform the operation.

    if (empty($error_msg)) {

        // Create hashed password using the password_hash function.
        // This function salts it with a random salt and can be verified with
        // the password_verify function.
        $password = password_hash($password, PASSWORD_BCRYPT);

        // Insert the new user into the database 
        if ($insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password) VALUES (?, ?, ?)")) {
            $insert_stmt->bind_param('ssss', $username, $email, $password); // THIS IS LINE 88
            // Execute the prepared query.
            if (! $insert_stmt->execute()) {
                // header('Location: ../error.php?err=Registration failure: INSERT');
                // exit;
                trigger_error("there was an error....".$mysqli->error, E_USER_WARNING);
            }
        }
        else (header('Location: ../register_success.php'));
        exit;
    }
}

来源:http://www.wikihow.com/Create-a-Secure-Login-Script-in-PHP-and-MySQL

最佳答案

bind_param('ssss', $username, $email, $password); 
            ^^^^        ^        ^        ^
            ||||        |        |        |
        4 parameters    1        2        3          4?

4 !== 3

关于php - 错误:mysqli_stmt::bind_param():类型定义字符串中的元素数量与绑定(bind)变量的数量不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35385844/

相关文章:

php - 使用SEARCH函数过滤现有表时出错

php - nginx 可以处理重复的 X-Forwarded-For header 吗?

php - 无法使用 PHP 和 MySQL 执行 MySQL 查询

php - Mysqli 不显示所有行

php - 使用用户名和密码字段中输入的任何内容进行脚本登录

PHP 表单不向 MySQL 发送数据

php - 自定义帖子类型永久链接返回 404

php - mysqli 查询结果未插入数据库

SQL查询的PHP大量内存使用

php - 部署到灵活的 PHP App Engine 时访问 Cloud SQL