PDA

View Full Version : Unique Data Names Question


JimNayzium
07-23-2008, 09:53 PM
I want to write a simple submit form.

have it hit a mySql database using PHP.

I want the form to be VERY simple.

NAME: -- submit

and if the name is already taken in the database for it to refresh with an error thrown...

write now it errors out in mySQL cuz I have the field set to UNIQUE in phpMyAdmin---

but I want to echo back to the user something besides..

Duplicate entry "Name" for key 2

derzok
07-24-2008, 04:59 PM
$name = mysql_real_escape_string($_POST['name']);
$result = mysql_query("SELECT * FROM database name WHERE id='$name'");
if(mysql_num_rows($result) > 0) {
// Your error message here
echo 'That user already exists!';
return; // don't forget to return or the code will keep going...
}
// the rest of your code

JimNayzium
07-24-2008, 05:06 PM
Awesome -- THANKS -- I will try this.

To save me the agony -- where in the mix does this go?

At the very beginning of my INSERT statements...

meaning just before the INSERT code?

So whenever I am running the insert statement run this bit first right?