gpt4 book ai didi

php - 关于重复键更新语法 php/mysql 错误

转载 作者:可可西里 更新时间:2023-11-01 07:38:55 26 4
gpt4 key购买 nike

我正在尝试执行一个查询,如果“ID”存在则更新,如果不存在则插入。当尝试运行以下查询时,我可以看到插入了如此多的重复记录。我不知道我做错了什么。我的查询如下。 Id 是'Autoincrement' 和'KEY'。我没有收到任何查询错误。数据库是Mysql

    while ($i < $size) {
$sl= $_POST['sl'][$i];
$item_id= $_POST['item_id'][$i];
$item_name= $_POST['item_name'][$i];
$prod_description=$_POST['prod_description'][$i];
$prod_description= mysql_real_escape_string($prod_description);
$item_quantity= $_POST['item_quantity'][$i];
$item_units= $_POST['item_units'][$i];
$unitprice= $_POST['unitprice'][$i];
$total=$_POST['total'][$i];
$currency_selected=$_POST['currency_change'][$i];
$total_inr= $_POST['total_inr'][$i];
$id = $_POST['id'][$i];
$item_quantity_sup= $_POST['item_quantity_sup'][$i];
$slab_range= $_POST['slab_range'][$i];
$item_units_sup= $_POST['item_units_sup'][$i];
$item_partno= $_POST['item_partno'][$i];
$ifmain= $_POST['ifmain'][$i];
$sup_itempartno = $_POST['sup_itempartno'][$i];

$query = "INSERT INTO comparitive_st_sup (
id,
tender_id,
item_id,
ifmain,
slno,
item_name,
item_partno,
prod_description,
sup_itempartno,
currency,
slab_range,
qty,
total_inr,
qty_sup,
item_units,
item_units_sup,
unitprice,
total,
supplier_name
)
VALUES (
$id,
'$tender_id',
'$item_id',
'$ifmain',
'$sl',
'$item_name',
'$item_partno',
'$prod_description',
'$sup_itempartno',
'$currency_selected',
'$slab_range',
'$item_quantity',
'$total_inr',
'$item_quantity_sup',
'$item_units',
'$item_units_sup',
'$unitprice',
'$total',
'$supplier_name2'
)
ON DUPLICATE KEY UPDATE
ifmain='$ifmain',
slno = '$sl',
item_name = '$item_name',
item_partno = '$item_partno',
prod_description = '$prod_description',
sup_itempartno = '$sup_itempartno',
currency = '$currency_selected',
slab_range= '$slab_range',
qty = '$item_quantity',
qty_sup = '$item_quantity_sup',
item_units = '$item_units',
item_units_sup = '$item_units_sup',
unitprice = '$unitprice',
total = '$total',
total_inr='$total_inr'";

mysql_query($query) or die ("Error in query: $query");
++$i;
}

实际场景是。我的数据库中有一些记录。我在编辑模式下调用记录。在这种模式下,用户可以添加一些行(动态行)并且他们可以更新。因此,应更新存在的记录,并将新添加的行插入到表中。

最佳答案

INSERT ON DUPLICATE KEY UPDATE 查询将在更新之前插入,因此如果您的唯一键是自动递增的,它将首先插入一条新记录,然后查找更新,但因为它永远不会相同,它永远不会更新。

关于php - 关于重复键更新语法 php/mysql 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36150582/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com