[英]Delete and Insert record at the same time PHP
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
谢谢
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. PHP中的
mysql
支持不允许在一个查询中使用多个statemets,因此您无法执行删除行并更新同一查询的任务。
You could use a INSERT... OR UPDATE
statement instead of DELETE
and INSERT
to get close to your task you like to achieve. 您可以使用
INSERT... OR UPDATE
语句代替DELETE
和INSERT
来完成您要完成的任务。
http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html http://dev.mysql.com/doc/refman/5.7/en/insert-on-duplicate.html
Eg 例如
INSERT INTO productcolor(productinformationID,colorName) VALUES("foo", "bar") ON DUPLICATE KEY UPDATE productinformationID=VALUES(productinformationID), colorName=VALUES(colorName)
在重复键更新上插入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.