duanjian4331 2016-10-19 22:06
浏览 28
已采纳

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

Right now I am trying to set up a user registration page, and I've been having trouble with verifying that the e-mail is not already in use. I figured all I had to do was make a query to my database to check and see if the e-mail was already in use. This seems pretty straight forward, so I don't know why it's giving me such a problem.

I've read several posts, and tried several approaches with PDO and mysqli, but I have still yet to get this script to function properly. Any help would be greatly appreciated.

<?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条回答 默认 最新

  • douzen1896 2016-10-19 22:27
    关注

    When doing COUNT(*) the server(MySQL) will only allocate memory to store the result of the count and its faster too.

    this part of your code that must be corrected:

    $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];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档