通过使用按钮更新表中的值



所以我有一个项目列表,每个项目都有一个删除和更新按钮,删除将从数据库中删除行,更新按钮将更改数量。

但问题是,我只能更新或删除列表中的最后一项。我尝试了很多方法,但都没有解决办法。有什么建议吗?

sql查询和表单:

$totaloftotal=0;
try{   
require('connection.php');
$sql="select i.item_name, c.qty, i.item_price, c.iid, i.item_photo, i.item_qty, c.cart_id FROM items i, cart c WHERE i.item_id=c.iid and uid=23 ";
$rs=$db->query($sql);

if($rs->rowCount()==0){
echo"CART EMPTY!!";
} else{
?>
<thead>
<tr>
<th>Update</th>
<th>Remove</th>
<th>Image</th>
<th>Product Name</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<tr>
<?php
while($row = $rs->fetch()){
$total=0;                                                               
$total+=$row[2];
?>

<form method="post" action="updatecart.php"> 
<td><button class="btn" type="submit" name="update" >Update</button></td>
<td><button class="btn" type="submit" name="remove" >Remove</button></td>
<?php echo"$row[1]"?>
<input type="hidden" name="itemid" value=<?php echo"$row[3]"?>>                                                             
<input type="hidden" name="cartid" value=<?php echo"$row[6]"?>>                                                             
<td>
<a href="product_detail.html">
<img alt="" src="photos/<?php echo"$row[4]"?> " width= 100 height=100>
</a>
</td>
<td><?php echo"$row[0]"?></td>                                                              
<td>
<input type="number" id="quantity" name="quantity" min=1 max=<?php echo"$row[5]"?> value=<?php echo"$row[1]"?>></td>    td>
<?php echo"$row[2]"?><</td>                                                         
<?php $total = $total * $row[1]?>
<td><?php echo"$total"?></td>
</tr>
<?php $totaloftotal+=$total;?>
<?php
}
//echo"$totaloftotal";
}
} catch ( PDOException $e ){
die($e->getMessage() );
}
?>  

这里是updatecart.php,它应该进行更改:

<?php
print_r($_POST);
extract($_POST);

if(isset($update)){

try{
require('connection.php');
$qty= $_POST["quantity"];
$itemid= $_POST["itemid"];
$cartid= $_POST["cartid"];
$qty= intval($qty);
$sql2= "update cart set qty=$qty where iid=$itemid  and uid=23";
$x = $db->exec($sql2);
$db=null;
header("location:cart.php");
}catch (PDOException $e){
die( $e->getMessage() );
}   
}
else{
try{
require('connection.php');
$sql2= "delete from cart where iid=$itemid";
$x = $db->exec($sql2);
$db=null;
header("location:cart.php");
}catch ( PDOException $e ){
die($e->getMessage());
}   
}
?>

尝试使用while:和endwhile

...
<tbody>
<tr>
<?php
while($row = $rs->fetch()): 
?>
<?php 
$total=0;                                                               
$total+=$row[2];
?>
<form method="post" action="updatecart.php"> 
<td> <button class="btn" type="submit" name="update" >Update</button></td>
<td> <button class="btn" type="submit" name="remove" >Remove</button></td>
<?php echo"$row[1]"?>
<input type="hidden" name="itemid" value="<?php echo $row[3]?>">                                                             
<input type="hidden" name="cartid" value="<?php echo $row[6]?>">                                                             
<td><a href="product_detail.html"><img alt="" src="photos/<?php echo"$row[4]"?> " width= 100 height=100></a></td>               <td><?php echo"$row[0]"?>                                               
</td>                                                               
<td><input type="number" id="quantity" name="quantity" min=1 max=<?php echo"$row[5]"?> value=<?php echo"$row[1]"?>></td>    td><?php echo"$row[2]"?><</td>                                                          
<?php $total = $total * $row[1]?>                                                       <td><?php echo"$total"?></td>
</tr>
<?php $totaloftotal+=$total;?>
<?php endwhile ?>

//echo"$totaloftotal";
.....

相关内容

  • 没有找到相关文章