首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >警告: mysqli_stmt_bind_param():类型定义字符串中的元素数与中的绑定变量数不匹配

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

Stack Overflow用户
提问于 2019-01-18 03:04:22
回答 1查看 104关注 0票数 -1

我正在用Php和MySqli做一个登录系统,我只是完成了注册功能。

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

代码语言:javascript
复制
<?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);
}

前面提到的error-array被用来以div的形式向页面发送视觉反馈,代码如下:

代码语言:javascript
复制
<?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 ?>
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54242636

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档