首页 > 解决方案 > 使用按钮更新表中的值

问题描述

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

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

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());
        }   
    }

?>

标签: phphtmlsqldatabase

解决方案


尝试使用 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";

.....

推荐阅读