Php 在更新时添加新行
我有一个动态行的表单。 我可以添加行并将它们全部保存到数据库中。 我还可以编辑表单并将值更新到数据库。 问题是,我无法在更新时添加新行 我的桌子是:Php 在更新时添加新行,php,mysql,Php,Mysql,我有一个动态行的表单。 我可以添加行并将它们全部保存到数据库中。 我还可以编辑表单并将值更新到数据库。 问题是,我无法在更新时添加新行 我的桌子是: 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
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`)
这是创建发票的页面,我可以在其中添加任意多的行。
注意输入类型=隐藏名称=订单id
$id = mysql_insert_id();
for($i = 0 ;$i < count($_POST['product_name']);$i++)
{
mysql_query("INSERT INTO tbl_orderdetail
SET order_id = '{$id}',
product_name = '{$_POST['product_name'][$i]}',
quantity = '{$_POST['quantity'][$i]}',
price = '{$_POST['price'][$i]}',
discount = '{$_POST['discount'][$i]}',
amount = '{$_POST['amount'][$i]}'
");
}
<tbody id="orderdetail" class="detail">
<tr>
<input type="hidden" name="order_id" >
<td width="2%" class="no">1</td>
<td width="10%"><input type="text" class="form-control quantity" name="quantity[]"></td>
<td width="60%"><input type="text" class="form-control product_name" name="product_name[]"></td>
<td width="8%"><input type="text" class="form-control price" name="price[]"></td>
<td width="4%"><input type="text" class="form-control discount" name="discount[]"></td>
<td width="10%"><input type="text" class="form-control amount" name="amount[]"></td>
<td width="6%"><a href="#" class="remove">Excluir</td>
</tr>
</tbody>
这是编辑页面。
注意
我相信这应该是订单id,但我需要它是id,因为更新
<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>
</table>
<input type="submit" class="btn btn-primary" name="update" id="update" value="Salvar">
</form>
<?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];
$query = ("UPDATE tbl_orderdetail SET product_name='$product_name', quantity='$quantity', price='$price', discount='$discount', amount='$amount' WHERE id = '$id'");
mysql_query($query) or die ("Error in query: $query");
++$i;
}
header( "Location: consulta_orc.php");
mysql_close();
}
?>
您可能需要研究在重复密钥更新时使用insert
如果未提供orderid,这将插入新行,但如果orderid与语句一起存在,则将更新现有行。有关详细信息,请参阅以下链接:您的查询将仅更新表中存在id的行。您需要按id执行搜索,如果id确实存在,请执行更新,否则请插入新行
$id = 1;
$stmt = $pdo -> prepare('select id from table where id = ?');
$stmt -> execute(array($id));
$rows = $stmt -> fetch(PDO::FETCH_ASSOC);
if (count($rows) > 0) {
echo 'update row';
} else {
echo 'insert row';
}
希望这有帮助,它可能需要一些更改来适应您的场景,但应该会给您一个解决问题的方法。如果可以,您应该这样做。它们不再得到维护,而是在使用。学习一下,然后考虑使用PDO,谢谢你们两个。我从在线教程中学习,mysql比mysqli教程多得多。为什么要添加行update@SatenderK,因为它是发票,所以在需要更新时可能必须添加一行。