【问题标题】:updating values in a table by using a button使用按钮更新表中的值
【发布时间】:2020-12-26 09:42:31
【问题描述】:

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

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

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

?>

【问题讨论】:

  • 详细了解 ajax 请求...
  • 如果您在代码中保持良好的格式/缩进,您会帮自己一个大忙。对上面的内容进行了编辑以使其清晰易读,您可以清楚地看到这些代码部分存在错误 ~ &gt;&lt;/td&gt; td&gt;?&gt;&lt;&lt;/td&gt; 更不用说您不能像这里一样合法地嵌套 html 元素。

标签: php html sql database


【解决方案1】:

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

.....

【讨论】:

  • 但我的问题不在于循环。问题是我只能更新列表的最后一行。
  • 你确定 $row[3] 包含正确的 itemid 并且这个值被发送到 updatecart.php ???
  • 是的,row[3] 包含 itemid。我添加了 print_r($_POST) 以查看发生了什么,项目 ID 是一个数组。也许这就是为什么它只更改最后一行?
  • 它已经包含在问题中,亲爱的,我已经修复了它。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-07-27
  • 1970-01-01
  • 2012-11-11
  • 2021-10-12
  • 2021-04-17
  • 2020-12-14
  • 1970-01-01
相关资源
最近更新 更多