Stopping the duplicates?

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!

Moderator: General Moderators

Post Reply
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Stopping the duplicates?

Post 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)
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Thanks...

Post by Joe »

Hey man thanks a lot... It worked a treat 8)

Regards


Joe
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

no problem :)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply