View Full Version : How to alet user if MySQL data inserted is not unique.

01-04-2007, 03:22 PM
Currently, if someone enters data in a unique column that is not unique, it just looks like it was inserted, but it wasn't.

How do I do something like this:

if ([Data inserted] is not unique){
echo '';

I know I could do a foreach loop and check it against every value, but since MySQL already knows it's unique, how do I tell this to the user?

01-04-2007, 06:01 PM
if a new row is inserted you can use mysql_insert_id() function to retrieve the id of the inserted row. If no row was inserted it will return null.

echo "Row inserted";
echo "Row was not inserted, duplicate content";

Also i think mysql_error() holds a message if you try to enter unique data, however displaying that to the user can be a security risk should there ever happen any other errors.

So if you really want to check if a value exists already a simple query beforehand is probably the best way to go, but who knows i dont consider myself a sql pro :D

01-04-2007, 06:09 PM
thanks! That worked perfectly