2011-03-30 63 views
0

我有更新帳戶信息的表單。在一種形式下,我有姓名,用戶名,電子郵件,公司,密碼。比方說,我想要做的就是改變我的名字,我仍然使用這種形式,所有其他領域保持不變。在更新表單中,我有一個電子郵件和用戶名檢查程序,確保它們尚未被使用。所以現在,即使我不更改我的電子郵件地址或用戶名,它也會被提交給檢查代碼。如何防止第二次重複檢查?

如何解決它,這樣它不會阻止它,如果它屬於同一個人:

// checks if the email is in use 
    if (!get_magic_quotes_gpc()) { 
    $_POST['email'] = addslashes($_POST['email']); 
    } 
    $emailcheck = $_POST['email']; 
    $check = mysql_query("SELECT email FROM accounts WHERE email = '$emailcheck'") 
    or die(mysql_error()); 
    $check2 = mysql_num_rows($check); 

    //if the name exists it gives an error 
    if ($check2 != 0) { 
    print("here i basically show the form once again highlighting the error)"); 
    die(''); 
    } 
+0

在旁註;你希望在列上使用'UNIQUE'索引嗎? – Wrikken 2011-03-30 20:02:18

回答

1

我不知道烏爾DB是如何被格式化,但用戶名添加到SQL應該這樣做

// checks if the email is in use 
    if (!get_magic_quotes_gpc()) { 
    $_POST['email'] = addslashes($_POST['email']); 
    } 
    $emailcheck = $_POST['email']; 
    $check = mysql_query("SELECT email FROM accounts WHERE email = '$emailcheck' 
         AND NOT(user='$username')") 
    or die(mysql_error()); 
    $check2 = mysql_num_rows($check); 

    //if the name exists it gives an error 
    if ($check2 != 0) { 
    print("here i basically show the form once again highlighting the error)"); 
    die(''); 
    } 

因此它的外觀在電子郵件不是特定用戶的:-)

+0

$用戶名在第二個變量檢查​​?對? – AAA 2011-03-30 20:03:52

+0

謝謝兄弟明白了。 – AAA 2011-03-30 20:09:32

+0

任何時候都不可能有@AAA^_ ^ – Neal 2011-03-30 20:09:55

1

存儲是提交表單的原始值的行中,並加載窗體時檢查爲new value == old value。如果不相等,請檢查,否則跳過。

<? 
    if ($_POST['oldemail'] != $_POST['newemail']) { 
     //Do check here 
    } 
    //so on and so on 
?>