如何在注册时使用php存在用户名存在或电子邮件存在的错误

时间:2017-03-03 12:38:49

标签: php mysql registration

嗨朋友们正在尝试显示错误,因为用户名已经存在或者在使用php注册时存在电子邮件。这是我的代码..

   if (isset($_POST['username']) && isset($_POST['password'])){
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];

    $unique_user = "SELECT * FROM `user` where username='$username' or email='$email'";
    $unique_result = mysqli_query($mysql, $unique_user);
    if(mysqli_num_rows($unique_result)>0)
       {
           echo "try again";
       }

现在正在重新打印,但我该如何检查是否已存在用户名或电子邮件

2 个答案:

答案 0 :(得分:1)

$unique_user = "SELECT * FROM `user` where username='$username' or email='$email'";
$unique_result = mysqli_query($mysql, $unique_user);                
if(mysqli_num_rows($unique_result)>0) {
       while ($row = mysqli_fetch_array($unique_result)) {
              if ($row['username'] === $username) {
                 echo 'Username Exist!';
              } else {
                 echo 'Email Exist!';
              }
       }
}

答案 1 :(得分:0)

我建议您只显示用户名或电子邮件,而不是用户名存在且电子邮件存在。 无论如何这里是代码:

       if (isset($_POST['username']) && isset($_POST['password'])){
        $username = $_POST['username'];
        $email = $_POST['email'];
        $password = $_POST['password'];

        $unique_user = "SELECT * FROM `user` where username='$username' or email='$email'";
        $unique_result = mysqli_query($mysql, $unique_user);
        if(mysqli_num_rows($unique_result)>0)
        {
          while($res = mysqli_fetch_assoc($unique_result))
          {
              if($res['username']  == $username)
              {
              echo "Username Exists";
              }
              if($res['email']  == $email)
              {
              echo "Email Exists";
              }
          }
          echo "Please try again";
        }