Page 1 of 1

Checking duplicate users

Posted: Wed Jul 31, 2002 8:22 am
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

Posted: Wed Jul 31, 2002 8:51 am
by Taikonaut
$query "select name from table where name=$name";
$result=mysql_query($query);
$count=mysql_num_rows($result);

if ($count>0) {.........

Posted: Wed Jul 31, 2002 9:42 am
by bedcor
Thank you Taikonaut! :D

That code works great!

Thanks again.

Posted: Wed Jul 31, 2002 10:05 am
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;

Posted: Wed Jul 31, 2002 10:07 am
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.

Posted: Wed Jul 31, 2002 10:10 am
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 :)

Posted: Wed Jul 31, 2002 10:23 am
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.