Forum Moderators: coopster

Message Too Old, No Replies

Duplicate Entries

MySQL Database that needs to check for duplicate entries

         

coertli

1:20 am on Jan 25, 2006 (gmt 0)

10+ Year Member



So I am pretty new to this whole PHP and MYSQL thing. I have a sql table called MovieDB that has a column called 'name'. I need to check to see if when someone enters a movie into the database, if that movie already exists. if it does, i will not add it. It should be pretty simple. so far my code is

$duplicate = mysql_query("SELECT * FROM MovieDB where name='$name'");

Where $name is what the person enters into the form. Should this be in quotes? And what do I do with result to see if it returns anything? Thanks for the help.

IamStang

3:56 am on Jan 25, 2006 (gmt 0)

10+ Year Member



Not sure about the gurus around here, but I use something similar to this:

$duplicate = mysql_query("SELECT * FROM MovieDB where name='$name'");
$affected_rows = mysql_num_rows($duplicate);
if($affected_rows >= 1)
{
// Tell them it is duplicate, show error, or whatever you want here
}
else
}
// Insert into database, or whatever.
}

It might need a bit of tweaking depending on how you are connecting to the database, etc. But the jist of it is there.

Hope it helps.

Oh, and as far as the quotes go, try it with and without. I use the quotes as that is how I was shown to do it. Never tried it without em.

Regards,
IamStang

coertli

5:33 am on Jan 25, 2006 (gmt 0)

10+ Year Member



I tried that code but i get a warning saying that mysql_num_rows is not a valid result

coertli

6:01 am on Jan 25, 2006 (gmt 0)

10+ Year Member



n/m I got it. Thanks for the help. Works great!