验证电子邮件尚未注册PHP Mysql

时间:2016-10-19 22:06:14

标签: php mysql mysqli pdo

目前我正在尝试设置用户注册页面,而且我在验证电子邮件尚未使用时遇到了问题。我认为我所要做的就是查询我的数据库以查看电子邮件是否已被使用。这看起来非常简单,所以我不知道它为什么会给我这样的问题。

我已经阅读了几篇帖子,并尝试了几种PDO和mysqli方法,但我还没有让这个脚本正常运行。任何帮助将不胜感激。

<?php
session_start();
if( isset($_SESSION['user_id']) ){
    header("Location: /");
}
require 'database.php';
$message = '';
if(!empty($_POST['email']) && !empty($_POST['password'])&& !empty($_POST['firstname'])&& !empty($_POST['lastname'])&& !empty($_POST['phone'])&& !empty($_POST['address'])&& !empty($_POST['city'])&& !empty($_POST['zip'])):

//check to see if e-mail is already being used
    //This method always says that the email is already in use, even if I am entering a new one. 
    /*
    $records = $conn->prepare('SELECT * FROM users WHERE email = :email');
    $records->bindParam(':email', $_POST['email']);
    $records->execute();
    $results = $records->fetch(PDO::FETCH_ASSOC);
    if( count($results) > 0){
        $message = "Sorry, that E-mail address is already registered to an account.";
    }
    */
    //this one never says that the email is in use.
    /*
    $email = $_POST['email'];
    $query = mysqli_query($conn, "SELECT * FROM users WHERE email='".$email."'");

    if(mysqli_num_rows($query) > 0){
        $message = "Sorry, that E-mail address is already registered to an account.";
    }
    */

    //this was the last method I tried, and it also never says that the email is in use.
    try{
        $stmt2 = $conn->prepare('SELECT `email` FROM `user` WHERE email = ?');
        $stmt2->bindParam(1, $_POST['email']); 
        $stmt2->execute();
        while($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
        }
    }
    catch(PDOException $e){
        echo 'ERROR: ' . $e->getMessage();
    }

    if($stmt2->rowCount() > 0){
        //echo "The record exists!";
        $message = "Sorry, that E-mail address is already registered to an account.";
    }

    else{
        // Enter the new user in the database
        $sql = "INSERT INTO users (email, password, firstname, lastname, phone, address, city, zip) VALUES (:email, :password, :firstname, :lastname, :phone, :address, :city, :zip)";
        $stmt = $conn->prepare($sql);

        $stmt->bindParam(':email', $_POST['email']);
        $stmt->bindParam(':password', password_hash($_POST['password'], PASSWORD_BCRYPT));
        $stmt->bindParam(':firstname', $_POST['firstname']);
        $stmt->bindParam(':lastname', $_POST['lastname']);
        $stmt->bindParam(':phone', $_POST['phone']);
        $stmt->bindParam(':address', $_POST['address']);
        $stmt->bindParam(':city', $_POST['city']);
        $stmt->bindParam(':zip', $_POST['zip']);

        if( $stmt->execute() ):
            $message = 'Successfully created new user';
        else:
            $message = 'Sorry there must have been an issue creating your account';
        endif;
    }

endif;

?>

1 个答案:

答案 0 :(得分:0)

当执行 COUNT(*)时,服务器(MySQL)只会分配内存来存储计数结果,也会更快。

您必须更正的代码部分:

$records = $conn->prepare('SELECT count(*) FROM users WHERE email = :email');
$records->bindParam(':email', $_POST['email']);
$records->execute();
$results = $records->fetch(PDO::FETCH_NUM);
echo $results[0];