Page 1 of 1

Stopping the duplicates?

Posted: Mon Mar 08, 2004 10:06 am
by Joe
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.


Regards



Joe 8)

Posted: Mon Mar 08, 2004 10:13 am
by malcolmboston
run a query such as

Code: Select all

$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
}
hope that helps

Thanks...

Posted: Mon Mar 08, 2004 10:21 am
by Joe
Hey man thanks a lot... It worked a treat 8)

Regards


Joe

Posted: Mon Mar 08, 2004 10:24 am
by malcolmboston
no problem :)

Posted: Mon Mar 08, 2004 2:51 pm
by pickle
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.