2

what is a problem with this code. I cannot go into insert function if table is empty, it is always doing update. Can anyone help me to solve this thing?

  while ($row = $result->fetch_object()) {

         if ($result->num_rows > 0) {

               $updatequery = sprintf("UPDATE stat_mailings SET cat_id='%s', mailing_name='%s', mailing_unique_id='%s', segment_id='%s', 
                                                                        campaign_id='%s', landing_page='%s', total_sent='%s' WHERE mailing_id='%s'",
                                        $row->category_id,
                                        mysqli_real_escape_string($mysqli, $row->mailing_naam),
                                        $row->unique_id,
                                        $row->mailing_segment,
                                        mysqli_real_escape_string($mysqli, $row->utm_campaign),
                                        mysqli_real_escape_string($mysqli, $row->landing_page),
                                        $row->mailing_total_sent, 
                                        $row->id                                           
                );

                 $mysqli->query($updatequery);
                 echo $error = $mysqli->error;   


        } else {

           $insertquery = sprintf("INSERT INTO stat_mailings SET mailing_id='%s', cat_id='%s', mailing_name='%s', mailing_unique_id='%s', segment_id='%s', 
                                                                        campaign_id='%s', landing_page='%s', total_sent='%s'",
                                        $row->id,
                                        $row->category_id,
                                        mysqli_real_escape_string($mysqli, $row->mailing_naam),
                                        $row->unique_id,
                                        $row->mailing_segment,
                                        mysqli_real_escape_string($mysqli, $row->utm_campaign),
                                        mysqli_real_escape_string($mysqli, $row->landing_page),
                                        $row->mailing_total_sent                                                                                       
                );

                 $mysqli->query($insertquery);
                 echo $error = $mysqli->error;      

        }
4
  • 2
    $result->num_rows is always greater than 0 .Check the value of $result->num_rows Commented Feb 11, 2013 at 10:57
  • Yup, i know that, but how to solve this, that it will work ? I cant figure it out .. Commented Feb 11, 2013 at 11:00
  • As I said check the values. your query always returnes more than 0 rows and the code does not get into insert part Commented Feb 11, 2013 at 11:03
  • Remove while loop and try. Commented Feb 11, 2013 at 11:09

1 Answer 1

4

Mysql has great feature exactly for this purpose - ON DUPLICATE query.
It looks like

INSERT INTO t SET f1=val,f2=val2 ON DUPLICATE KEY UPDATE f1=val,f2=val2

and so on.
Only make sure you have an UNIQUE index on the key field.
So, you can get rid of your SELECT query and it's result checking.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.