Click here to Skip to main content
15,942,934 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">
                    <th> Shoe name</th>
                    <th> Shoe image </th>
                    <th> Quantity</th>
                    <th> Total price</th>
                    <th> Remove Item</th>
                    <th colspan="2"> Operations</th>

                <!-- PHP to display dynamic datas from cart-->
              global $conn;
              $get_ip_address = getIPAddress();  
              $cart_query="select * from `cart_details` where ip_address='$get_ip_address'";
                $select_products="select * from `products` where shoe_id='$shoe_id'";
                    <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 -->
                    $get_ip_address = getIPAddress();  
                        $update_cart="update `cart_details` set quantity=$quantities where ip_address='$get_ip_address'";

                    <td><?php echo $price_table?></td>
                    <td><input type="checkbox" name="removeitem[]" value="<?php echo $shoe_id?>"></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">
                <?php }} ?>
        <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>
Updated 24-May-23 2:12am
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.

1 solution

I having that same problem here..
Share this answer
Richard Deeming 11-Oct-23 8:54am    
"Me too" is not a solution to someone else's question.

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