...

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



Crazydog
01-04-2007, 02: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?

vegu
01-04-2007, 05: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.



mysql_query($sql_that_inserts_row_maybe);
if(mysql_insert_id())
echo "Row inserted";
else
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

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum