简体   繁体   中英

How to check if email or username are registered

I want to check if email is already in database too but I doesnt work with this code.

I am using this code to check for the name:

   $checkDB = "SELECT user_name FROM users WHERE user_name='$name'";
    $result = mysqli_query($conn, $checkDB);
    $row = mysqli_fetch_array($result, MYSQLI_ASSOC);

    if (mysqli_num_rows($result) == 1) {
        $errorName = 'Това име е заето';
        $mainError = true;
    }

What you should do is set a constraint in your database for unique usernames and e-mail addresses. Then, try to do an insert and catch the exception when it fails. Otherwise, you could have a condition where nearly simultaneous users try to register at the same time, and between your SELECT and INSERT statements, the username or e-mail address might be used by someone else.

ALTER TABLE users ADD UNIQUE INDEX email (email);

or you can check both with SQL first:

$sql = mysqli_query($conn, "SELECT * FROM users WHERE user_name='$name' OR email='$email' LIMIT 1");
if(mysqli_num_rows($sql)>=1){
    $errorName = "Username or email already exist.";
    $mainError = true;
}

I think it will works

$w=mysqli_query($con,"select * from users where user_name='$name'");
$num=mysqli_num_rows($w);

if($num>=1)
{
echo "Record Already Exists";   
}
else
 {
  echo "No Record";
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM