gpt4 book ai didi

php - 插入 |在重复键更新上添加新行

转载 作者:行者123 更新时间:2023-11-30 22:36:39 25 4
gpt4 key购买 nike

我正在尝试更新一个表,使其能够添加新行。
该脚本正在更新值,但未插入新行。当我添加一行时,它会更新第一行。

这是我的 table :

 TABLE `tbl_orderdetail` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`order_id` int(11) DEFAULT NULL,
`product_name` varchar(255) DEFAULT NULL,
`quantity` varchar(255) DEFAULT NULL,
`price` varchar(255) DEFAULT NULL,
`discount` int(11) DEFAULT NULL,
`amount` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)

这是我的更新代码:

<?php
if (isset($_POST['update'])) {

$size = count($_POST['quantity']);
$size = count($_POST['product_name']);
$size = count($_POST['price']);
$size = count($_POST['discount']);
$size = count($_POST['amount']);

$i = 0;
while ($i<$size) {
$quantity= $_POST['quantity'][$i];
$product_name= $_POST['product_name'][$i];
$price= $_POST['price'][$i];
$discount= $_POST['discount'][$i];
$amount= $_POST['amount'][$i];
$id= $_POST['id'][$i];
$order_id= $_POST['order_id'][$i];



$query = ("INSERT INTO tbl_orderdetail (id, order_id, quantity, product_name, price, discount, amount )
VALUES ('$id', '$order_id', '$quantity', '$product_name', '$price', '$discount', '$amount' )
ON DUPLICATE KEY UPDATE quantity = '$quantity', product_name = '$product_name', price = '$price', discount = '$discount', amount = '$amount'");


mysql_query($query) or die(mysql_error());
++$i;



}
header("Location: $PHP_SELF");
mysql_close();
}
?>

这是我的表格:

<form   method="post" action="">    

<div class="box-body">
<table class="table table-bordered table-hover">

<?php
$sql = "SELECT * FROM tbl_orderdetail WHERE order_id=$id";

$result = mysql_query($sql);

$count=mysql_num_rows($result);

?>




<thead>
<th>No</th>
<th>Qtde</th>
<th>Descrição</th>
<th>Unitário</th>
<th>Desc.(%)</th>
<th>Valor</th>
<th><input type="button" value="+" id="add" class="btn btn-primary"></th>

</thead>


<tbody id="orderdetail" class="detail">

<?php

while ($rows = mysql_fetch_array($result)){

?>

<tr>
<input type="hidden" name="id[]" value="<?php echo $rows['id']; ?>" >
<td width="2%" class="no">1</td>
<td width="10%"><input type="text" id="quantity" class="form-control quantity" name="quantity[]" value="<?php echo $rows['quantity']; ?>"></td>
<td width="60%"><input type="text" id="product_name" class="form-control product_name" name="product_name[]" value="<?php echo $rows['product_name']; ?>"></td>
<td width="8%"><input type="text" id="price" class="form-control price" name="price[]" value="<?php echo $rows['price']; ?>"></td>
<td width="4%"><input type="text" id="discount" class="form-control discount" name="discount[]" value="<?php echo $rows['discount']; ?>"></td>
<td width="10%"><input type="text" id="amount" class="form-control amount" name="amount[]" value="<?php echo $rows['amount']; ?>"></td>
<td width="6%"><a href="#" class="remove">Excluir</td>
</tr>
<?php } ?>
</tbody>


<tfoot>
<th></th>
<th></th>
<th></th>
<th></th>
<th style="text-align:center;" >Total R$</th>
<th style="text-align:center;" class="total">0</th>
</tfoot>

</table>
<input type="submit" class="btn btn-primary" name="update" id="update" value="Salvar">

</form>

最佳答案

首先:不要使用php的mysql库,改用mysqli或PDO

第二:您的代码容易受到 sql 注入(inject)的攻击 - 最好的解决方法是使用准备好的语句(我再次建议查看 PDO)。永远不要相信可能来自用户的任何东西(即所有 POST、GET 东西)。

第三:对于开发,始终显示所有错误、警告、通知等(您可能至少已经看到 mysql 库的弃用警告 - 如果使用 php >= 5.5 - 以及不小心使用 $_POST 值)

第四:很抱歉,您的代码对我来说意义不大:

1.)

$size = count($_POST['quantity']);
$size = count($_POST['product_name']);
// ... etc ...
$size = count($_POST['amount']);

这里你只考虑了amount的大小。所以所有在最后一行之前带有 $size = 的行都是无意义的。

2.) 在 while 循环中,我们发现类似的行:

$id= $_POST['id'][$i];

并且 $i 递增直到它的大小为 $_POST['amount']。如果 $_POST['id'] 的大小与 $_POST['amount'] 的大小不同怎么办?

3.) 您正在为主键使用 auto_increment 但手动插入 id。我会在这里删除 auto_increment。

因此,如果我猜的话,我会尝试回显 $query 并检查 while 循环是否没有按应有的频率运行,或者 id 值是否有某种奇怪的地方。

== 编辑 ==

我刚刚看到你的 html 输出。正如 J.Han 指出的那样:您的脚本完全按照它所说的去做:您正在从数据库中读取所有结果,在编辑并提交它之后,新值应该会覆盖旧值。

如果你想将该脚本与插入一起使用,你必须将 id 设置为尚未在你的数据库中的一个(即在 html 中设置一个零值并在 php 部分捕获它生成一个有效的新 id)

关于php - 插入 |在重复键更新上添加新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32361636/

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