Checking duplicate users

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
bedcor
Forum Newbie
Posts: 24
Joined: Fri Jul 26, 2002 11:01 am
Location: Barrie, Ontario

Checking duplicate users

Post by bedcor »

Can someone steer me in the right direction?, On my form, once the user has entered his information and pressed the "Save" button i want my code to check if that name already exists, and if it does spit out a message and if it doesn't then add the new user.

This is where i would like to insert that piece of code:
[syntax=php]if ($_POST['button'] == 'Save') {

$query = "INSERT INTO clientinfo (client_id, contact_name, position, department, street, city, prov_state, country, zip_postal, phNumber, faxNumber, pagerNumber, email) VALUES '$client_id', '$contact_name', '$position', '$department', '$street', '$city', '$prov_state', '$country', '$zip_postal', '$phNumber', '$faxNumber', '$pagerNumber', '$email');";

}[/syntax]
Thnx
Taikonaut
Forum Newbie
Posts: 2
Joined: Wed Jul 31, 2002 8:51 am
Location: Gelsenkirchen, Germany

Post by Taikonaut »

$query "select name from table where name=$name";
$result=mysql_query($query);
$count=mysql_num_rows($result);

if ($count>0) {.........
bedcor
Forum Newbie
Posts: 24
Joined: Fri Jul 26, 2002 11:01 am
Location: Barrie, Ontario

Post by bedcor »

Thank you Taikonaut! :D

That code works great!

Thanks again.
fatalcure
Forum Contributor
Posts: 141
Joined: Thu Jul 04, 2002 12:57 pm
Contact:

Post by fatalcure »

or:

Code: Select all

$query = "INSERT INTO table (id, name, address) VALUES($id, '$name', '$address') WHERE name <> '$name'";
if (!$result = mysql_query($query)) &#123;
     echo "Username taken";
&#125;
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

You've got to be real careful with that logic, fatalcure. You can't be sure that the query failed because a user already existed, and you don't want to start missing mysql errors that are occurring.
fatalcure
Forum Contributor
Posts: 141
Joined: Thu Jul 04, 2002 12:57 pm
Contact:

Post by fatalcure »

llimllib wrote:You've got to be real careful with that logic, fatalcure. You can't be sure that the query failed because a user already existed, and you don't want to start missing mysql errors that are occurring.
hum? The query has to fail if the user already exists no? explain this to me :)
User avatar
llimllib
Moderator
Posts: 466
Joined: Mon Jul 01, 2002 2:19 pm
Location: Baltimore, MD

Post by llimllib »

yes, the query will fail if the user exists. However, if there is any problem with the query - say, for example, the table structure changes - the script will return "user already exists" which is untrue. This can make debugging a royal pain the the butt later, when your program gives you an untrue error message - because you told it to.
Post Reply