简体   繁体   English

如何检查电子邮件或用户名是否已注册

[英]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. 否则,您可能会遇到以下情况:几乎同时使用的用户尝试同时注册,并且在SELECT和INSERT语句之间,其他人可能会使用用户名或电子邮件地址。

ALTER TABLE users ADD UNIQUE INDEX email (email);

or you can check both with SQL first: 或者您可以先使用SQL进行检查:

$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";
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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