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 Quote Link to comment 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')"; } Quote Link to comment 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... Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.