簡體   English   中英

同時刪除和插入記錄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支持不允許在一個查詢中使用多個statemets,因此您無法執行刪除行並更新同一查詢的任務。

您可以使用INSERT... OR UPDATE語句代替DELETEINSERT來完成您要完成的任務。

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

例如

在重復鍵更新上插入productcolor(productinformationID,colorName)VALUES(“ foo”,“ bar”)到productinformationID = VALUES(productinformationID),colorName = VALUES(colorName)

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

            ?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM