简体   繁体   中英

Warning: mysqli_stmt_bind_param(): Number of elements in type definition string doesn't match number of bind variables in

I am doing a Login system with Php and MySqli and I am just finishing the Sign Up feature.

After having set error_reporting(E_ALL), I get the aforementioned error, one of the two bugs I get is even if my if-statements do not go through the input is till being sent to the database. This is my code:

<?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);
}

The aforementioned error-array is being used to send visual feedback in the form of a div to the page with the following code:

<?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 ?>

If your goal is to determine if a username already exists, you shouldn't be concerned with the password.

You've got this:

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

You want this:

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

Also note, you need to check the return status of the insert query. In a multi-user system, it's possible for another user to INSERT a record in between the time that the SELECT runs and the INSERT runs. So you need to ensure that your database has a UNIQUE constraint on the username field, and then check that your INSERT succeeded.

When you're later checking if a user's provided password is the correct one, you'll need to select the password:

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

That will return the hashed password, which you can then compare against what the user typed:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM