Hello Codeproject,
It's my first two weeks of learning HTML, SQL, and PHP. I just finished following a tutorial on a simple register/login system. The simplicity however came with a cost; they did not show you how to check if a username already exists.
The registering and logging in works, but, I'm having troubles implementing my check if username exists.
This is the code that I'm having troubles with:
$sql="SELECT FROM members WHERE username=123";
if(mysql_num_rows($sql)>=1)
{
echo"name already exists";
}
else
This is the code that registers the user(After the above code):
{
$q = "INSERT INTO `members` (`username`,`password`,`email`) "
."VALUES ('".$_POST["username"]."', "
."PASSWORD('".$_POST["password"]."'), "
."'".$_POST["email"]."')";
$r = mysql_query($q);
if ( !mysql_insert_id() )
{
die("Error: User not added to database.");
}
else
{
Header("Location: register.php?op=thanks");
}
}
}
Could anyone tell me what I'm doing wrong with the "select from" query? I registered around 20 accounts with the username as "123" to see if it was just me messing up the username input, but still no luck!
Edit: Silly me! I forgot to use mysql_query(), this would have never given any result from my database. Fixed code:
$sql=mysql_query("SELECT * FROM `members` WHERE `Username` = 123");
if(mysql_num_rows($sql) != 0) {
echo"name already exists";
} else {}
Best regards,
- Dimitri