Database not checking if username is free [closed]

我的梦境 提交于 2019-12-13 09:38:06

问题


Okay, I have another problem, the code is fixed now:

{ 
   //Check if user already exists 
    $un_check = mysql_query("SELECT Username FROM users WHERE Username = '$un'");  
    if(mysql_num_rows($un_check) >0) {
echo "Username already exists";
}
else{
 // Username Free
}

But the database still allows usernames to be the same. Also, even though it says "username already exists" I still creates the account anyway?

any ideas?


回答1:


try this

$un_check = mysql_query("SELECT Username FROM users WHERE Username = 'POSTVALUE'");  
if(mysql_num_rows($un_check) >0) {
   echo "Username is taken";
}
else{
 // Username Free
}



回答2:


Seems typo change $un to $un_check,

if(mysql_num_rows($un_check) >0) {...

Always have your PHP errors turn on at-least on development environment,

error_reporting(E_ALL);

Note: Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.




回答3:


Change this :-

if(mysql_num_rows($un) >0)

to

if(mysql_num_rows($un_check) >0)


来源:https://stackoverflow.com/questions/17298295/database-not-checking-if-username-is-free

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!