我在PHP中有一个用户名检查,当'username'已经存在时,每当有人输入值'username2'时,它就会返回它已被占用。
include 'connect.php';
$username = $_POST['r_username'];
$password = $_POST['r_password'];
$email = $_POST['r_email'];
$qry = "SELECT * FROM users WHERE 'username' = '$username'";
if(mysql_query($qry) != ''){
echo "<script type='text/javascript'>
alert('This username has already been taken!');
window.location = 'register.php';
</script>";
}else{...
感谢您的帮助!
答案 0 :(得分:2)
$qry = "SELECT * FROM users WHERE 'username' = '$username'";
我对php或MySql了解不多,但只是想问一下field_name应该是'username'还是没有引号。可能是它将其作为值而不是column_name。我不确定
答案 1 :(得分:1)
$query_response = mysql_query($qry)
if (mysql_num_rows($query_response) > 0) {
echo "<script type='text/javascript'>
alert('This username has already been taken!');
window.location = 'register.php';
</script>";
}
else {
echo 'This username has not been taken yet. Please, proceed';
}
看一下mysql_query返回的内容。您的条件(mysql_query($qry) != ''
)将始终为true
此外,使用反引号代替撇号(')将查询中的列名括起来。现在,您将$ username字符串与字符串username
进行比较,而不是username
列。
"SELECT * FROM users WHERE `username` = '".mysql_real_escape_string($username)."'";
答案 2 :(得分:1)
您错误地使用了mysql_query()
。它返回一个要在其他函数中使用的资源。