更新购物车表中的数量

时间:2016-08-14 07:38:58

标签: javascript php html

我希望更新按钮应该在我选择的那个字段中。但它只出现在第一行并且正在更新第一行数量字段。

<form action="cart.php?action=update" method="post">

<table>
<tr>
    <th colspan="2">ITEM</th>
    <th>QUANTITY</th>
    <th>PRICE</th>
    <th>SUBTOTAL</th>
    <th>REMOVE</th>

</tr>
<?php
        $query = "select * from cart where customer_id='$user' ";
        $result = mysqli_query($con,$query);$b = 0;$c = 0;
        while($row = mysqli_fetch_array($result))
        {
                $productid = $row['product_id'];
                $query2 = "select * from product where product_id='$productid'";
                $result2 = mysqli_query($con,$query2);
                while($row2=mysqli_fetch_array($result2))
                {
?>
    <tr>

        <td rowspan="3"><img src="upload/<?php echo $row2['pimage']; ?>" height="50px" width="50px"></td>
        <td rowspan="3"><?php echo $row2['pname']; ?></td>
        <td rowspan="3">

        <input tpe="text"  name="newqty" value="<?php echo $qty = $row['quantity']; ?>" onkeypress="showsubmit()">
        <input style="visibility:hidden;width:80px;border-radius:10px;background-color:green;border:none;padding:5px;color:white;" type="submit" name="sub1" id="sub1" value="UPDATE">
        <input type="hidden" name="hidcartid" value="<?php echo $row['cart_id'] ?>"/>
        <input type="hidden" name="hidproductid" value="<?php echo $row['product_id']; ?>"/>
        <script>
            function showsubmit()
            {

                document.getElementById("sub1").style.visibility = "visible";
            }

        </script>


        </td>

        <td>Price:<?php  echo $sp  = $row2['psellingprice']; ?></td>
        <?php
                $total = $sp * $qty;
        ?>
        <td rowspan="3">
        <?php 
            echo $t = $total;
            $b = $b + $t;


        ?></td>
        <td rowspan="3"><a href="cart.php?action=delete&cid=<?php echo $row['cart_id']; ?>"">REMOVE</a></td>
    </tr>
    <?php 
        $action = ( array_key_exists( 'action', $_REQUEST) ? $_REQUEST['action'] : "" );

        if($action =="delete")
        {

            deletecart($_REQUEST['cid']);

        }
        if($action=="update")
        {
            echo "update function called";
            updatecart();
            echo "update function executed";
        }

    ?>
    <tr>
        <td>Selling Price:<?php echo $p =  $row2['pprice']; ?></td>

    </tr>
    <tr>
        <td>You Saved:
        <?php 

            $d = $row2['pdiscount'];
             $s = ($p*$d)/100;
            echo $q = $s * $qty;
                    $c = $c + $q;


         ?>&nbsp; rs.</td>
    </tr>
<?php
                }
        }

?>
</table>

和cart2.php

<?php

function deletecart($cartid)
{
    include 'connection.php';

    $sql1="delete from cart where cart_id=$cartid";
    $executequery = mysqli_query($con,$sql1);
    header('location:cart.php');
}
function updatecart()
{
    include 'connection.php';
    $cartId     = $_POST['hidcartid'];
    $productId  = $_POST['hidproductid'];
    $newqty = $_POST['newqty'];

    echo("inside update function");


            // update product quantity
        $sql = "UPDATE cart
                SET quantity = $newqty
                WHERE cart_id = $cartId";


        mysqli_query($con,$sql);
        header('location:cart.php');
    }?>

对于行onw一切都很好。但是对于row2更新按钮出现在row1中而根本没有更新。

1 个答案:

答案 0 :(得分:0)

<input tpe="text"  name="newqty" value="<?php echo $qty = $row['quantity']; ?>" onkeypress="showsubmit(<?php echo $row['cart_id'] ?>)">

<input style="visibility:hidden;width:80px;border-radius:10px;background-color:green;border:none;padding:5px;color:white;"
               type="submit"
               name="sub_<?php echo $row['cart_id'] ?>"
               id="sub_<?php echo $row['cart_id'] ?>"
               value="UPDATE">

<script>
    function showsubmit(id)
    {
                document.getElementById("sub_"+id).style.visibility ="visible";
    }
</script>