Click here to Skip to main content
15,566,625 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. I am having hard time fixing my current code which requires to update both items I have in cart. (For example I have 2 items in my cart, then my code doesn't update its quantity if the both items isn't filled out)
My desired output: May I ask for help in how can I update the quantity of the selected item in my cart without requiring the other if ever I have 2 or more cart items?

Thank you my code is pasted below

What I have tried:

<div class="container">
    <div class="row">
        <form action="" method="post">
        <table class="table table-border text-center w-auto m-auto">
            <thead>
                <tr>
                    <th> Shoe name</th>
                    <th> Shoe image </th>
                    <th> Quantity</th>
                    <th> Total price</th>
                    <th> Remove Item</th>
                    <th colspan="2"> Operations</th>
                </tr>
            </thead>
            <tbody>


                <!-- PHP to display dynamic datas from cart-->
                <?php
              global $conn;
              $get_ip_address = getIPAddress();  
              $total_price=0;
              $cart_query="select * from `cart_details` where ip_address='$get_ip_address'";
              $result=mysqli_query($conn,$cart_query);
              while($row=mysqli_fetch_array($result)){
                $shoe_id=$row['shoe_id'];
                $select_products="select * from `products` where shoe_id='$shoe_id'";
                $result_shoes=mysqli_query($conn,$select_products);
                while($row_shoe_price=mysqli_fetch_array($result_shoes)){
                  $shoe_price=array($row_shoe_price['shoe_price']);
                  $price_table=$row_shoe_price['shoe_price'];
                  $shoe_name=$row_shoe_price['shoe_name'];
                  $shoe_image1=$row_shoe_price['shoe_image1'];
                  $shoe_values=array_sum($shoe_price);
                  $total_price+=$shoe_values;
                ?>
                <tr>
                    <td><?php echo $shoe_name?></td>
                    <td><img src="./admin_area/product_images/<?php echo $shoe_image1?>" class="cart_img"alt=""></td>
                    <td><input type="text" name="qty" class="form-input w-50"></td>
                    
<!-- Code for update cart -->
                    <?php 
                    $get_ip_address = getIPAddress();  
                    if(isset($_POST['update_item'])){
                        $quantities=$_POST['qty'];
                        $update_cart="update `cart_details` set quantity=$quantities where ip_address='$get_ip_address'";
                        $result_shoe_quantity=mysqli_query($conn,$update_cart);
                        $total_price=$total_price*$quantities;
                    }?>

                    
                    <td><?php echo $price_table?></td>
                    <td><input type="checkbox" name="removeitem[]" value="<?php echo $shoe_id?>"></td>
                    <td>
                    <!--<button class="bg-primary text-light m-2 px-3 border-0">update</button>-->
                    <input type="submit" value="Update Item" class="bg-primary text-light m-2 px-3 border-0" name="update_item">
                    <input type="submit" value="Remove Item" class="bg-primary text-light m-2 px-3 border-0" name="remove_item">
                    </td>
             
                </tr>
                <?php }} ?>
            </tbody>
        </table>
        <div class="d-flex">
            <h4 class="px-3" style="font-size: 20px;">Total Amount: PHP <?php echo $total_price?></h4>
            <a href="shopnow.php"><button class="bg-secondary text-light m-2 px-3 border-0">Continue Shopping</button></a>
            <a href="#"><button class="bg-primary text-light m-2 px-3 border-0">Proceed to Checkout</button></a>
        </div>
    </div>
</div>
</form>
Posted
Updated 17-Nov-22 2:18am
v3
Comments
Member 15627495 20-Nov-22 9:02am    
hello ! to improve your code, really make a separation between Html side, and Php scripts.
you will gain on readability.
you know the global $myvar. use global vars deeply.
create one file for your php. with all the codes in, and through your html side, just do with 'echo $the_var_one;"
how to do that ? make an 'require("myphpFile.php"); at top of your html pages.
Member 15627495 20-Nov-22 9:11am    
the code lines like :
$the_var = $row['field']; // or
$the_var = $_POST['field']
are useless. It's just reallocation of value, a swap between two var. It's useless to have values in redundant dublets.

if you don't apply a function in those types of code lines, it's code for nothing. really.prefer to 'use a var as it is'.
Member 15627495 20-Nov-22 9:27am    
don't doubt about the 'db sql engine', you can pass query with hundred of lines and parameters.
for your script, do you prefer this query :
select * from `products` where shoe_id IN ( select * from `cart_details` where ip_address='$get_ip_address() ); 
do you see where it goes ? one loop less.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900