2012-11-11 47 views
0

我在更新数据库中的多行时遇到问题。这是我的形式。SQL只更新最后一行

<form action="results-action" method="post" enctype="multipart/form-data"> 
<fieldset> 

<table id ="table_id" class="display"> 

<thead> 

<tr><td><h2>Pending Order</h2></td></tr> 

<tr> 

<th scope="col">Order ID</th> 
<th scope="col">Name</th> 
<th scope="col">Address</th> 
<th scope="col">Product Name</th> 
<th scope="col">Produt Quantity</th> 
<th scope="col">Price</th> 
<th scope="col">Order status</th> 
</tr> 
</thead> 

<tbody> 

<?php 

while ($row = mysqli_fetch_array($result)) { 
?> 

<tr> 

<td><input type="text" value='<?=$row['virtuemart_order_id']?>' name="orderid" id="virtuemart_order_id"></td> 

<td><?=$row['first_name']?></td> 
<td><?=$row['address_1']?></td> 
<td><?=$row['order_item_name']?></td> 
<td><?=$row['product_quantity']?></td> 
<td><?=$row['product_final_price'] ?></td> 

<td><select name='change'> 

<option value='P'> Pending</option> 

<option value='C'> Confirmed</option></select></td> 

</tr> 

<?php 

} 

?> 

</tbody> 
</table> 
</fieldset> 

<fieldset> 
<table> 
<tr> 
<td><input type="submit" value="Update status" name="update status"> </td> 
</tr> 
</table> 
</fieldset> 

</form> 

当我更改订单状态以确认每个行时,它应该将订单状态更新为每行的'P'。但是,它只更新最新的行。

这是我的更新代码。

$change = $_POST["change"]; 
$orderid = $_POST['orderid']; 

// build SQL statement 
$query = "update ruj3d_virtuemart_order_items set order_status= '$change' WHERE virtuemart_order_id = '$orderid'"; 

// execute SQL statement 
$status = mysqli_query($link, $query) or die(mysqli_error($link)); 

if ($status) { 
echo "<big>It has been updated !!. </big>"; 

}else { 
echo "<br>Has not been successfully confirmed</br>"; 
} 
+0

我已经删除了大量不必要的空格中。在我看来,你的HTML仍然不可读。你能减少你发布的数量吗?它甚至相关吗? – Ben

+0

所有ids必须是唯一的,你知道。 – Daedalus

回答

1

更改您的下拉菜单,如下

<select name='change[<?=$row['virtuemart_order_id']?>]'> 
    <option value='P'> Pending</option> 
    <option value='C'> Confirmed</option> 
</select> 

然后在你的PHP,

foreach($_POST['change'] as $orderid => $change) { 
    $query = "update ruj3d_virtuemart_order_items set order_status= '$change' WHERE virtuemart_order_id = '$orderid'"; 

    // execute SQL statement 
    $status = mysqli_query($link, $query) or die(mysqli_error($link)); 
} 
+0

嗨,谢谢!有用 !! 这也可以用于插入查询以及? 例如.. 因为我将订单状态更新为多个订单的挂单权。 我需要插入一些已经改变的历史记录。在称为'评论'的数据库字段中,与订单4一样已被确认。而且,因为我更新了超过1行,可以'foreach'的工作是一样的吗? –

+0

它也适用于插入查询。坦克! –