警告:mysqli_stmt_bind_param():类型定义字符串中的元素数与中的绑定变量数不匹配

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (159)

我正在使用Php和MySqli进行登录系统,我刚刚完成了注册功能。

在设置了error_reporting(E_ALL)之后,我得到了上面提到的错误,即使我的if语句没有通过输入直到被发送到数据库,我得到的两个错误之一。这是我的代码:

<?php

require_once "partials/header.php";
error_reporting(E_ALL);

if (isset($_POST["signup-button"])) {
require "includes/dbconn.php";

$username = filter_var($_POST["username"], FILTER_SANITIZE_STRING);
$username = trim($username);

$email = filter_var($_POST["email"], FILTER_VALIDATE_EMAIL);
$email = trim($email);

$password = filter_var($_POST["password"], FILTER_SANITIZE_STRING);
$passwordRe = filter_var($_POST["passwordRe"], FILTER_SANITIZE_STRING);

$termsOfService = filter_var($_POST["terms"], FILTER_SANITIZE_STRING);
$errors = [];

if (strlen($username) < 5) {
    $errors[] = "Your Username should contain at least 5 characters";
}

if (!$email) {
    $errors[] = "Your E-Mail is invalid.";
}

if (strlen($password) < 6) {
    $errors[] = "Your password should contain at least 6 characters.";
}

if ($password !== $passwordRe) {
    $errors[] = "Both passwords should be identical";
}

if (!$termsOfService) {
    $errors[] = "Please accept our Terms of Service";
} else {
    /*$sql = "INSERT INTO usersvet (userName, email, password) VALUES
                    ('" . $username . "', '" . $email . "', '" . $hashedPwd . "')";

        if (!mysqli_query($dbConnection, $sql)) {
            die(mysqli_error($dbConnection));*/
    $sql = "SELECT userName FROM usersvet WHERE userName=? AND pwd=?";
    $statement = mysqli_stmt_init($dbConnection);

        if (!mysqli_stmt_prepare($statement, $sql)) {
            $errors[] = "Sql Error.";
        } else {
            mysqli_stmt_bind_param($statement, "s", $username);
            mysqli_stmt_execute($statement);
            mysqli_stmt_store_result($statement);
            $resultCheck = mysqli_stmt_num_rows($statement);

            if ($resultCheck > 0) {
                $errors[] = "User already taken.";                 
            } else {
                $sql = "INSERT INTO usersvet (userName, email, pwd)
                                            VALUES (?, ?, ?);";
                $statement = mysqli_stmt_init($dbConnection);

                if (!mysqli_stmt_prepare($statement, $sql)) {
                    $errors[] = "SQL Error.";
                } else {
                    $hashedPwd = password_hash($password, PASSWORD_DEFAULT);
                    mysqli_stmt_bind_param($statement, "sss", $username, $email, $hashedPwd);
                    mysqli_stmt_execute($statement);
                    mysqli_stmt_store_result($statement);
                }
            }
        }
    }
    mysqli_stmt_close($statement);
    mysqli_close($dbConnection);
}

前面提到的错误数组用于使用以下代码以div的形式向页面发送视觉反馈:

<?php if (!empty($errors)): ?>
    <?php foreach ($errors as $error): ?>
        <div class="well">
            <p class="alert alert-warning"><?= $error ?></p>
        </div>
    <?php endforeach ?>
<?php endif ?>

<?php if (!empty($_POST) && empty($errors)): ?>
        <div class="well">
            <p class="alert alert-success">Sign Up successful. You can now login <a href="login.php">here</a>.</p>
        </div>
<?php endif ?>
提问于
用户回答回答于

如果您的目标是确定用户名是否已存在,则不应该关注密码。

你有这个:

$sql = "SELECT userName FROM usersvet WHERE userName=? AND pwd=?";
mysqli_stmt_bind_param($statement, "s", $username);

你要这个:

$sql = "SELECT userName FROM usersvet WHERE userName=?";
mysqli_stmt_bind_param($statement, "s", $username);

另请注意,您需要检查插入查询的返回状态。在多用户系统中,另一个用户可以在SELECT运行和INSERT运行之间插入记录。因此,您需要确保数据库在用户名字段上具有UNIQUE约束,然后检查INSERT是否成功。

当您稍后检查用户提供的密码是否正确时,您需要选择密码:

$sql = "SELECT pwd FROM usersvet WHERE userName=?";
mysqli_stmt_bind_param($statement, "s", $username);

这将返回哈希密码,然后您可以将其与用户键入的内容进行比较:

if (password_verify($typedPassword, $hashedPasswordFromDatabase)) {
    // correct password provided
} else {
    // wrong password provided
}

扫码关注云+社区

领取腾讯云代金券