Jump to content

PHP update quantity in shopping cart help !


bouriche

Recommended Posts

thank you for helping me

here is my code i need help in quantity of products in shopping cart:

</tr>
 
                      <?php
                      $total = 0;
 
                      global $con;
 
                      $ip = getIp();
 
                      $sel_price = "select * from cart where ip_add='$ip'";
 
                      $run_price = mysqli_query($con, $sel_price);
 
                      while ($p_price=mysqli_fetch_array($run_price)) {
                        $pro_id = $p_price['p_id'];
                        
                        $pro_price = "select * from products where product_id='$pro_id'";
                        
                        $run_pro_price = mysqli_query($con, $pro_price);
 
                        while ($pp_price=mysqli_fetch_array($run_pro_price)) {
                          
                          $product_price = array($pp_price['product_price']);
                          $product_title = $pp_price['product_title'];
                          $product_image = $pp_price['product_image'];
                          $single_price = $pp_price['product_price'];
                          
 
                          $values = array_sum($product_price);
                          $total += $values;
 
                       
 
 
                      ?>
                          <tr>
                            <td><input type="checkbox" name="remove[]" value="<?php echo $pro_id ?>"></td>
                            <td style="color:#222;"><?php echo "<a style='color:#222;text-decoration:none;' href='details.php?pro_id=$pro_id'> $product_title <br><img src='admin_area/product_images/$product_image' width='50'></a>" ?>
                            </td>
                            <td><input type="text" size="2" name="qty" value=""></td>
                            <?php
                            if (isset($_POST['update_cart'])) {
 
                              $qty = $_POST['qty'];
 
                              $update_qty = "update cart set qty='$qty'";
                              $run_qty = mysqli_query($con, $update_qty);
 
                              $total = $total*$qty;
                              
                            }
 
 
                            ?>
                            <td style="color:green;"><?php echo "$" . $single_price; ?></td>
                          </tr>
                          <?php } } ?>
                          <tr>
                            <td  style="background:white;border:none;"></td>
                            <td  style="background:white;border:none;"></td>
                            <td><b> Total   : </b></td>
                            <td style="color:green;padding:5px;"><?php echo "$" . $total ?></td>
                          </tr>
 
Link to comment
Share on other sites

there's a whole bunch of things wrong with this code that will prevent anyone from helping with it. some of the problems -

 

1) global $some_variable; only has meaning when used inside a function, where it shouldn't be used anyway since it leads to bad coding practices and, if this code is inside a function, that's a bad design since a function should only do one small thing.

 

2) you cannot (successfully) use the ip address to associate a cart with a user. if the user is logged in, you would use the user id. if the user isn't logged in, you would use the session id.

 

3) to display the contents of the cart, you would run one JOIN'ed query to get the cart and the product details at one time.

 

4) making the product price an array, then using array sum on an array containing one entry makes no sense. you would also want to take into account the quantity of the item when calculating the total of the cart. calculating the quantity times the running total inside the post method form processing code makes no sense.

 

5) any post method form processing code should be separate from your html markup and be near the top of your code file. it should also validate the submitted data (what if the value is negative number, what if the value is a zero) and use a prepared query to protect against sql injection. your update query is also missing an important part, which row(s) is it supposed to be updating? after successfully running the post method form processing code, you should do a header() redirect to the same page to cause a get request to re-display the page.

 

6) you would want to output the existing quantity as the value in the quantity form field so that the user can edit the quantity.

 

7) you would also process any remove checkboxes in the form processing code.

 

8 ) the form field for the quantity needs to be an array name and use the product id as the array key and you might as well have the 'remove' checkbox use the product id as the array key instead of as the value.

 

9) if the cart is empty, you would want to output a message stating so.

Edited by mac_gyver
Link to comment
Share on other sites

there's a whole bunch of things wrong with this code that will prevent anyone from helping with it. some of the problems -

 

1) global $some_variable; only has meaning when used inside a function, where it shouldn't be used anyway since it leads to bad coding practices and, if this code is inside a function, that's a bad design since a function should only do one small thing.

 

2) you cannot (successfully) use the ip address to associate a cart with a user. if the user is logged in, you would use the user id. if the user isn't logged in, you would use the session id.

 

3) to display the contents of the cart, you would run one JOIN'ed query to get the cart and the product details at one time.

 

4) making the product price an array, then using array sum on an array containing one entry makes no sense. you would also want to take into account the quantity of the item when calculating the total of the cart. calculating the quantity times the running total inside the post method form processing code makes no sense.

 

5) any post method form processing code should be separate from your html markup and be near the top of your code file. it should also validate the submitted data (what if the value is negative number, what if the value is a zero) and use a prepared query to protect against sql injection. your update query is also missing an important part, which row(s) is it supposed to be updating? after successfully running the post method form processing code, you should do a header() redirect to the same page to cause a get request to re-display the page.

 

6) you would want to output the existing quantity as the value in the quantity form field so that the user can edit the quantity.

 

7) you would also process any remove checkboxes in the form processing code.

 

8 ) the form field for the quantity needs to be an array name and use the product id as the array key and you might as well have the 'remove' checkbox use the product id as the array key instead of as the value.

 

9) if the cart is empty, you would want to output a message stating so.

thank you sir thats helped me a lot

all the best

if you want to contact me here is my facebook: https://www.facebook.com/abdenour.abdoun

and my email: bouricheabdenour@gmail.com

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.