2013-05-09 208 views
0

我有一個註冊表單的php代碼,它將詳細信息存儲到一個表(temp_members_db),然後將確認代碼發送到輸入的電子郵件。如果點擊代碼,temp_members_db的詳細信息將被傳輸到新表registered_members,如果用戶登錄,將從中檢查登錄詳細信息。避免重複條目

問題與註冊碼有關,我如何避免郵件重複和用戶名? 我的代碼是:

<?php 

include('config.php'); 

$tbl_name=temp_members_db; 

$confirm_code=md5(uniqid(rand())); 

$name=$_POST['name']; 
$email=$_POST['email']; 
$password=$_POST['password']; 
$password_confirm=$_POST['password_confirm']; 

$sql="INSERT INTO $tbl_name(confirm_code, name, email, password)VALUES('$confirm_code', '$name', '$email', '$password')"; 
$result=mysql_query($sql); 

if 
($password != $password_confirm) 
{ 
echo "Password do not match"; 
} 
else if 
($result) 
{ 

$to=$email; 

$subject="Your confirmation link here"; 

$header="from: Name <[email protected]>"; 

$message="Your Comfirmation link \r\n"; 
$message.="Click on this link to activate your account \r\n"; 
$message.="http://[email protected]/confirmation.php?passkey=$confirm_code"; 

$sentmail = mail($to,$subject,$message,$header); 

} 

else { 
echo "Email not found"; 
} 

if($sentmail){ 
echo "Your Confirmation link Has Been Sent To Your Email Address."; 
} 
else { 
echo "<BR/>Cannot send Confirmation link to your e-mail address"; 
} 

?> 

感謝:d

回答

0

運行SQL以確定是否姓名或電子郵件中的任何表已經存在INSERT進入數據庫之前。 如果查詢返回>0,則可以顯示錯誤消息。

例如,

SELECT COUNT(*) FROM temp_members_db, registered_members WHERE email = $email OR name = $name;

+0

謝謝,它的工作:) – Zalo 2013-05-09 06:07:55

+0

不客氣。請投票並將其標記爲接受的答案:) – 2013-05-09 06:12:50