PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
HI!. Recently I created a new database using mySQL and i am affiliating it with PHP. Now the sign-up script works great but I have no clue on how to stop others writing names which already exist and duplicating them. Is there any good tutorials or can someone please give a little advice on what i should do.
$usernamecheck = "SELECT username FROM users WHERE username = $_POST[username]";
$usernamequery = mysql_query($usernamequery);
$usernameexistence = mysql_num_rows($usernamequery);
if ($usernameexistence >= 1)
{
// this user already exists
print "do something to them and then make sure you call exit() to stop the user data from getting instered into the database";
exit();
}
else
{
// do nothing and just continue with the script, next should be the insert
record behaviour
}
Or you could make the username the primary key, then check the error code MySQL returns. There's a unique code for "key already exists", so you could check for that.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.