2015-05-09 48 views
0

是否可以將Username or Email already exists拆分爲特定的錯誤消息?用戶名或電子郵件已存在

我工作的這個用戶註冊腳本,如果您有任何關於給出的代碼,其餘任何提示,將不勝感激

if(isset($_POST['register'])){ 
    if( 
    // check if posts not empty 
    empty($_POST['username']) || 
    empty($_POST['email']) || 
    empty($_POST['password']) || 
    empty($_POST['re_password']) || 
    $_POST['password'] != $_POST['re_password'] 
    ){ 
    // if a field is empty, or the passwords don't match make a message 
    error = '<p>'; 
     if(empty($_POST['username'])){ 
     $error .= 'No username given<br>'; 
     } 
     if(empty($_POST['email'])){ 
      $error .= 'No email given<br>'; 
     } 
     if(empty($_POST['password'])){ 
      $error .= 'No password given<br>'; 
     } 
     if(empty($_POST['re_password'])){ 
      $error .= 'You must re-type your password<br>'; 
     } 
     if($_POST['password'] != $_POST['re_password']){ 
      $error .= 'Passwords don\'t match<br>'; 
     } 
     $error .= '</p>'; 
    } 
    else{ 
     $username = $_POST['username']; 
     $email = $_POST['email']; 
     $password = $_POST['password']; 
     $password = $_POST['re_password']; 
     $query = mysqli_query(
      $conn,"SELECT * FROM members WHERE username = '". $username ."' OR email = '". $email ."'"); 
     if(mysqli_num_rows($query) > 0){ 
      echo "Username or Email already exist"; 
     }  
     else { 
      $sql = "INSERT INTO members (username, password, email) 
        VALUES 
         ('$_POST[username]', 
         '$_POST[email]', 
         '$_POST[password]')"; 
      if (mysqli_query($conn, $sql)) { 
       echo "Registered"; 
      } else { 
       echo "Error: " . $sql . "<br>" . mysqli_error($conn); 
      } 
     } 
    } 
} 
if(isset($error)){ 
    echo $error; 
    unset($error); 
} 

回答

2

你只需要改變這裏: -

if(mysqli_num_rows($query) > 0){ 
    while ($row = mysqli_fetch_assoc($query)){ 

     if(isset($row['username']) && !empty($row['username']) && $_POST['username'] ==$row['username'])){ 
      echo "Username already exist"; 
     } 
     if(isset($row['password']) && !empty($row['password']) && $_POST['password'] ==$row['password'])){ 
     echo "Password already exist"; 
     } 
    } 
} 

注意: - 如果您的密碼有一些加密,那麼您需要相應地更改密碼部分條件(只是第三部分)。另外,如果我錯過了任何其他索引寫作讓我知道。

+0

是的,它的工作完美,謝謝分配 – skunkhaze

+0

然後請標記爲您的答案。它可以幫助他人輕鬆獲得解決方案。 –

相關問題