ainoy31 Posted July 3, 2007 Share Posted July 3, 2007 I am trying to make sure that I do not insert a duplicate record in the database. Here is my code: $sql = "INSERT INTO nHeal_printed(printed_id, order_id, gift_type, gift_first, gift_last, gift_email, first_name, last_name, phone, amount) VALUES ('', '$order_id', '$gift_type', '$gift_first', '$gift_last', '$gift_email', '$first_name', '$last_name', '$phone', '$amount') WHERE order_id NOT IN (SELECT order_id FROM nHeal_printed)"; mysql_query($sql, $cid) or die('Error: ' . mysql_error()); I get the following error: Error: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE order_id NOT IN (SELECT order_id FROM nHeal_printed)' at either it is the towards the end of the day or i am not seeing it. much appreciation AM Link to comment https://forums.phpfreaks.com/topic/58327-check-duplicate-record-in-mysql/ Share on other sites More sharing options...
per1os Posted July 3, 2007 Share Posted July 3, 2007 Mysql <= 3.x cannot do Subquerys. You have to branch it off into 2 seperate queries. $record = mysql_query("SELECT order_id FROM nHeal_printed WHERE order_id = '" . $order_id . "'"); if (mysql_num_rows($record) > 1) { echo 'Duplicate record'; }else { $sql = "INSERT INTO nHeal_printed(printed_id, order_id, gift_type, gift_first, gift_last, gift_email, first_name, last_name, phone, amount) VALUES ('', '$order_id', '$gift_type', '$gift_first', '$gift_last', '$gift_email', '$first_name', '$last_name', '$phone', '$amount')"; } Link to comment https://forums.phpfreaks.com/topic/58327-check-duplicate-record-in-mysql/#findComment-289195 Share on other sites More sharing options...
ikmyer Posted July 3, 2007 Share Posted July 3, 2007 couldn't you just make the field order_id unique in the database, then the insert will fail if duplicate... Link to comment https://forums.phpfreaks.com/topic/58327-check-duplicate-record-in-mysql/#findComment-289198 Share on other sites More sharing options...
ainoy31 Posted July 3, 2007 Author Share Posted July 3, 2007 Thank you for the that. However, in my situation, I need to insert into the table first then, check to see if there is a duplicate record. The reason why is because I am doing a site that emails people a link to where they can click and print the gift certificate. I am trying to avoid where people can click or copy the url and print the gift cert again over and over. Link to comment https://forums.phpfreaks.com/topic/58327-check-duplicate-record-in-mysql/#findComment-289217 Share on other sites More sharing options...
hackerkts Posted July 3, 2007 Share Posted July 3, 2007 ainoy31 try to read frost110 post, I think it should works. Link to comment https://forums.phpfreaks.com/topic/58327-check-duplicate-record-in-mysql/#findComment-289222 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.