2015-08-30 41 views
0

任何人都可以幫助我究竟在哪裏出錯本代碼?當我正在檢查數據庫中是否存在電子郵件時,我錯了。試圖檢查用戶輸入的郵件是否已在數據庫中

如何檢查數據庫中是否存在郵件?

 <html> 
     <h1> Registration Form </h1> 
     <body> 
     <form method="post" action=""> 
     <input type="text" name="fname" placeholder="first name" required><br><br> 
     <input type="text" name="lname" placeholder="last name" required><br><br> 
     <input type="text" name="mail" placeholder="mail" required> <br><br> 
     <input type="PASSWORD" name="pass1" placeholder="password" required> <br><br> 
     <input type="PASSWORD" name="pass2" placeholder="repeat password" required> <br> <br> 
     <input type="submit" name="submit"> 
     </form> 


     <?php 
     if(isset($_POST['submit'])) 
      { 
     $fname = $_POST['fname']; 
     $lname = $_POST['lname']; 
     $mail = $_POST['mail']; 
     $pass1 = $_POST['pass1']; 
     $pass2 = $_POST['pass2']; 
     if($pass1 !== $pass2) 
     { 
     echo "password in correct "; 
     } 
     else 
     { 
     $db = mysqli_connect("localhost","root","","reviewsystem"); 
     $check = "SELECT * FROM registers WHERE $mail = email"; 
     if($check == TRUE){ 
     echo "email already exists"; 
      } 
     else{ 
     $query = "INSERT INTO registers(fname,lname,email,password) VALUES('$fname','$lname','$mail','$pass1')"; 
     mysqli_query($db,$query); 
     echo " you are registered succesfully"; 
      } 
      } 
      } 
     ?> 


    </body> 
    </html> 

回答

1

首先要做的事情是:您沒有執行任何類型的查詢。你剛剛創建了一個字符串。添加一行:

$db = mysqli_connect("localhost","root","","reviewsystem"); 
$check = "SELECT * FROM registers WHERE $mail = email"; 
$result = mysqli_query($db, $check); //i think there is a typo, mysqi_query 

後,您可以使用mysqli_num_rows它返回來自DB回到識別該條目已經存在選擇行數:

if(mysqli_num_rows($result) > 0){ 
    echo "email already exists"; 
} 
+0

@KrishnaKamal是的,只是注意到查詢錯誤以及。將其更改爲'「SELECT * FROM registers WHERE email ='$ mail'」' – DannyPhantom

+0

現在完美。謝謝! @danny –

0

的問題是你的where子句中的mysql查詢。你必須寫它作爲

$check = "SELECT * FROM registers WHERE email = '".$mail."'"; if($check == TRUE){ echo "email already exists"; } else{ 
相關問題