1

Hi i am planning to delete and insert values at the same time when i click submit it will delete the old values and at the same time it will insert new values

Here is my code.

<?php    
if (isset($_POST['submit'])) {    
  $color1 = $_POST['color1'];
  $count = count($color1);       
  for ($x = 0; $x <=$count; $x++) {
    $savecolor = $color1[$x];
    $stmt = $db->prepare("DELETE FROM productcolor WHERE productinformationID =  :field0");
    $stmt->execute(array(':field0' => $prodID));    
    $stmt = $db->prepare("INSERT INTO productcolor(productinformationID,colorName) VALUES(:field0,:field00)");
    $stmt->execute(array(':field0' => $prodID, ':field00' => $savecolor));
   } 
} 
?>

It only delete my values there's no value saving in my database..

Please help me. Thanks

StephenKing
  • 36,187
  • 11
  • 83
  • 112
user5598179
  • 113
  • 1
  • 8

2 Answers2

0

The mysql support in PHP don't allow multiple statemets in one query, so your task to delete a row and update on the same query is not possible.

You could use a INSERT... OR UPDATE statement instead of DELETE and INSERT to get close to your task you like to achieve.

http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html

E.g

INSERT INTO productcolor(productinformationID,colorName) VALUES("foo", "bar") ON DUPLICATE KEY UPDATE productinformationID=VALUES(productinformationID), colorName=VALUES(colorName)

ins0
  • 3,918
  • 1
  • 20
  • 28
0
                  <?php
                global $wpdb;
            if(isset($_POST['submit'])) {
                    $checkBox =$_POST['chk1'];
                    $wpdb->query( "DELETE FROM wp_vandor WHERE parent_id = '".$user_id."'" );
                foreach($checkBox as $checkedfv){
                        $wpdb->query("INSERT INTO wp_vandor  (parent_id, child_id) VALUES ('".$user_id."', '".$checkedfv."')");
                    }
                    $crpurl = home_url('favorite-vendors');
                    header("Location: ".$crpurl);
              }

            ?>