简体   繁体   English

登录注册表格问题

[英]Login Registration Form Issue

I am making a login/registration form for my site. 我正在为我的网站制作一个登录/注册表格。 When I am registering as a new user with a new username though it reads as if that username already exists even though the database is empty. 当我使用新用户名注册为新用户时,即使数据库为空,它也会显示该用户名已经存在。 I'll try to walk you through the code on this but I am not sure where the issue is. 我将尝试引导您完成此过程的代码,但不确定问题出在哪里。

the register.php I have it where it checks the user input if it matches anything in the database it will output the proper error message and if there is no matches it will then go to the register function to register the user into the database. register.php我在其中检查用户输入是否与数据库中的任何内容匹配,它将输出正确的错误消息,如果不匹配,则将进入register函数将用户注册到数据库中。 Thank you :) 谢谢 :)

    if (isset($_POST['submit'])) {

    if(empty($_POST['username']) || empty($_POST['password']) || empty($_POST['email']) || empty($_POST['Fname'])){

        $errors[] = 'All fields are required.';

    }else{

        if ($users->user_exists($_POST['username']) === true) {
            $errors[] = 'That username already exists';
        }
        if(!ctype_alnum($_POST['username'])){
            $errors[] = 'Please enter a username with only alphabets and numbers';  
        }
        if (strlen($_POST['password']) <6){
            $errors[] = 'Your password must be atleast 6 characters';
        } else if (strlen($_POST['password']) >18){
            $errors[] = 'Your password cannot be more than 18 characters long';
        }
        if (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) === false) {
            $errors[] = 'Please enter a valid email address';
        }else if ($users->email_exists($_POST['email']) === true) {
            $errors[] = 'That email already exists.';
        }
    }

    if(empty($errors) === true){

        $username       = htmlentities($_POST['username']);
        $password       = $_POST['password'];
        $email          = htmlentities($_POST['email']);
        $firstName      = htmlentities($_POST['Fname']);
        $lastName       = htmlentities($_POST['Lname']);
        $accountType    = $_POST['account_type'];

        $users->register($username, $password, $email, $firstName, $lastName, $accountType);
        header('Location: register.php?success');
        exit();
    }
}

here is the functions Check to see if the the username and email already exists 这是功能检查用户名和电子邮件是否已经存在

public function user_exists($username) {

        $query = $this->mysqli->prepare("SELECT COUNT(`id`) FROM `users` WHERE `username`= ?");
        $query->bind_param('s', $username);

        $query->execute();
        $rows = $query->fetch();

        if($rows == 1){
            return true;
        }else{
            return false;
        }

    }

public function email_exists($email) {

    $query = $this->mysqli->prepare("SELECT COUNT(`id`) FROM `users` WHERE `email`= ?");
    $query->bind_param('s', $email);

        $query->execute();

        $rows = $query->fetch();

        if($rows == 1){
            return true;
        }else{
            return false;
        }

}

and the register function 和注册功能

public function register($username, $password, $email, $firstName, $lastName, $accountType){

    global $bcrypt; // making the $bcrypt variable global so we can use here

    $time       = time();
    $ip         = $_SERVER['REMOTE_ADDR']; // getting the users IP address
    $email_code = $email_code = uniqid('code_',true); // Creating a unique string.

    $password   = $bcrypt->genHash($password);

    $query  = $this->mysqli->prepare("INSERT INTO `users` (`username`, `password`, `email`, `ip`, `time`, `email_code`, `firstName`, `lastName`, `accountType`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ");

    $query->bind_param('ssssissss',$username, $password,  $email, $ip, $time, $email_code, $firstName, $lastName, $accountType);


    $query->execute();
            }

I believe your problem is this line in public function user_exists($username) - 我相信您的问题是public function user_exists($username)这一行-

$rows = $query->fetch();

$rows is not being set to the value from your query, but just returning true as the $query->fetch() succeeded. 没有将$rows设置为查询的值,而是仅在$query->fetch()成功$query->fetch()返回true Reading from the manual for mysqli_stmt::fetch - mysqli_stmt::fetch手册中mysqli_stmt::fetch -

all columns must be bound by the application before calling mysqli_stmt_fetch(). 在调用mysqli_stmt_fetch()之前,所有列都必须由应用程序绑定。

Try changing it to - 尝试将其更改为-

public function user_exists($username) {

    $query = $this->mysqli->prepare("SELECT COUNT(`id`) FROM `users` WHERE `username`= ?");
    $query->bind_param('s', $username);

    $query->execute();

    // Bind the results       
    $query->bind_result($count);

    while($rows = $query->fetch()){

       if($count == 1){
          return true;
       }else{
          return false;
       }

    }

}

you would also need to do this for public function email_exists($email) 您还需要对public function email_exists($email)执行此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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