Jump to content

Check duplicate record in mysql


ainoy31

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.