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

时间:2019-01-17 19:04:22

标签: php mysqli

我正在用Php和MySqli做一个Login系统,而我刚刚完成了Sign Up功能。

设置了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 ?>

1 个答案:

答案 0 :(得分:0)

如果您的目标是确定用户名是否已存在,则不必担心密码。

你有这个:

$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
}