简体   繁体   English

检查数据库中是否已经存在用户名或电子邮件

[英]Checking if a username or email exists in the DB already

I want to check if the username of email already exists in the DB when a user signs up to my website. 当用户注册我的网站时,我想检查数据库中是否已经存在电子邮件的用户名。 I have the following code which isn't working and i'm not sure why. 我有下面的代码不起作用,我不确定为什么。 If anyone could point me in the right direction to validate against already existing info in the DB I would greatly appreciate it! 如果有人能指出正确的方向来验证数据库中已有的信息,我将不胜感激! Thanks 谢谢

$query = mysql_query("
    SELECT * 
    FROM users 
    WHERE username = '". $username ."' 
        OR email = '". $email ."'"
);

if (mysql_num_rows($query) > 0){
    die ('Username or email already in use.');
}

This can be done by changing the username field as a primary key or unique. 这可以通过将用户名字段更改为主键或唯一键来完成。

Or run the sql 或者运行sql

SELECT username FROM table WHERE username='$php_username_var';

Please look at the following code, this is being used on the sign up page of my site, I hope you get your answer. 请查看以下代码,该代码正在我的网站的注册页面上使用,希望您能得到答复。

$sq="SELECT * FROM users WHERE username='$username' and password='$password'";
 $rsult=mysql_query($sq);      $count=mysql_num_rows ($rsult); 
if($count==1){echo "username already in use"; }

else 

{

$sql="INSERT INTO $tbl_name(username, password, sex, place)
VALUES('$username', '$password', '$sex', '$place')"; } 
$result=mysql_query($sql);     if($result)
{header("location:home.php"); }
else 
{ echo "Error, please go back and sign up again! "; }  ?> 

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

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