同时删除和插入记录PHP


Delete and Insert record at the same time PHP

嗨,我计划删除并插入值,同时单击提交时,它将删除旧值,同时插入新值

这是我的密码。

<?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));
   } 
} 
?>

它只删除我的值,没有值保存在我的数据库中。。

请帮帮我。谢谢

PHP中的mysql支持不允许在一个查询中有多个语句集,因此删除一行并更新同一查询的任务是不可能的。

您可以使用INSERT... OR UPDATE语句而不是DELETEINSERT来接近您想要实现的任务。

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

E.g

在复制密钥上插入productcolor(productioninformationID,colorName)VALUES("foo","bar")更新productioninformation ID=VALUES

                  <?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);
              }
            ?>