2016-10-19 63 views
0

現在我正在嘗試設置用戶註冊頁面,並且在驗證電子郵件尚未使用時遇到了問題。我想我所要做的就是查詢我的數據庫來檢查電子郵件是否已被使用。這看起來很直截了當,所以我不知道爲什麼它給了我這樣一個問題。確認電子郵件尚未註冊PHP Mysql

我讀過幾篇文章,並嘗試了幾種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; 

?> 
+0

你得到什麼錯誤? –

+0

嗨,我認爲bindParam需要三個參數,例如$ sth-> bindParam(':calories',$ calories,PDO :: PARAM_INT)。最後一個是數據類型。這可能會導致它無法正常工作。查看http://php.net/manual/en/pdostatement.bindparam.ph獲取更多信息 –

+0

嘗試'bindParam(':email',$ _POST [「email」],PDO :: PARAM_STR,100);'in你的第一次嘗試。 – Evochrome

回答

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]; 
+0

好吧真棒,謝謝!我更新了查詢並將if結果[0]放在if條件中,並解決了我的問題。 –

相關問題