How do I update quantity in cart?

I have been trying to get this to work for 3 weeks now and still nothing. what I want is that my users are able to update their quantiy form their items in the cart. how do I do this?

I have found one way to do it but it will only remember the last input from the users

code without styling

<?php 
include "config.php";
        ?>
        <div class="text-center" style="font-size: 100px;">&#128717;</div>
        <h2 class="text-center">Winkelmandje</h2><br>
        <section class="container content-section">
            <!-- <h2 class="section-header">CART</h2> -->
            <div class="cart-row">
                <span class="cart-item cart-header cart-column">ITEM</span>
                <span class="cart-item cart-header cart-column">PRICE</span>
                <span class="cart-item cart-header cart-column">QUANTITY</span>   
                <span class="cart-item cart-header cart-column">berekening</span>  
                <!-- <span class="cart-item cart-header cart-column">Verwijderen</span>  -->
            </div>  
            <?php
            $broodjes = $_GET['broodjes_ID'];
            
            if (isset($_SESSION['basket'])){
                if( in_array( $broodjes ,$_SESSION['basket']) )
                {
                    
                }else{
                    $_SESSION['basket'][] = $broodjes;
                   
                    
                }
            }else{
                $_SESSION['basket'][]= $broodjes;
               
                
            }

            
            $sumtotal = 0;
           
                     foreach($_SESSION['basket'] as $key => $value){

                         //echo "Key = $key; value = $value; <br>";
                         $sql = "SELECT broodjes_ID, broodnaam, prijs, voorraad FROM broodjes WHERE broodjes_ID=?";
                         $stmt = $conn->prepare($sql); 
                         $stmt->bind_param("i", $value);
                         $stmt->execute();
                         $result = $stmt->get_result();
                        
                         if($row = $result->fetch_assoc()){
                            
                     
                            echo '<div class="cart-items">';
                            echo '<div class="cart-row">';
                                echo '<div class="cart-item cart-column">';
                                    echo $row['broodnaam'];
                                echo '</div>';
                                echo '<div class="cart-item cart-column">';
                                    echo '€ ' . $row['prijs'];
                                echo '</div>';
                                //quantity
                                echo '<div class="cart-item cart-column">';
                                
                                  echo '<form method="POST" action="">';
                                     echo '<div class="col-xs-4">';
                                     echo '<input type="hidden" name="broodnaam" id="broodnaam" value="' . $row['broodnaam'] . '">';
                                     echo '<input type="number" name="quantity" id="quantity" class="form-control input-sm" value="1" min="1" max="'.$row['voorraad'].'">';
                                     echo '</div>';
                                  echo '</form>';
                                 echo '</div>';
                                 echo '<div class="cart-item cart-column">';
                                 echo '</div>';
                                
                                //  array_push($_SESSION['basket'], $_POST['quantity']);
                                //  var_dump($_SESSION['basket']);
                                
                       
                                $quantity = 1;
                                
                                        if (isset($_POST['quantity']) && !empty($_POST['quantity'])){
                                          
                                           $_SESSION['quantity'] = $_POST['quantity'];
                                          var_dump($_SESSION['quantity']);
                                   //           //$qty = $_SESSION["qty"] + 1;
                                           if (isset($_POST['broodnaam']) && !empty($_POST['broodnaam'])){
                                          
                                                  if ($_POST['broodnaam'] == $row['broodnaam']){
                                                        $quantity = $_POST['quantity'];
                                                        var_dump($quantity);
                                   //                    //array_push($_SESSION['qty'], $quantity);
                                   //                 //    var_dump($_SESSION['qty']);
                                               }
                                             }
                                         }

                         
                                    echo '<div class="cart-item cart-column">';
                                    $rowtotaal = $row['prijs'] * $quantity;
                                    $sumtotal += $rowtotaal;
                                    echo $rowtotaal;
                                    echo '</div>';

                            echo '</div>';
                            echo '</div>';

                          
                         }
                         
                        
                     } 
                     ?> <br />
               
                    <div class="cart-total">
                        <strong class="cart-total-title">Total</strong>
                        <span class="cart-total-price"> € <?php   echo $sumtotal;?></span>
                    </div>
                    <br/>

All that I know is that I have to (somehow) store quantity alongside the products the user has entered in their cart?
but idk how to do this?

The session cart (basket) should use the item id as the array index, and the quantity as the stored value. You are currently just adding the item id to the session cart.

When you display the cart, you would get all the item ids at once (see array_keys()), query to get all the matching item data (you can use find_in_set() to do this with one prepared-query bound input parameter), then loop over the rows that the query returns. For each item id, you would get the corresponding quantity from the session cart to use as the quantity form field’s value='...' attribute. You would have one single html form and the quantity field needs to be an array using the item id as the array index.

When you submit this form to update the quantities in the cart, you would first detect if a post method form was submitted, loop over the item ids in the cart, access the corresponding quantity element in the $_POST data, and store the newly submitted quantity into the session cart. For zero quantities, you need to decide if you will unset() the entry in the session cart or if you will store the zero value, then ignore it in later processing. This post method form processing code should be above the start of the html document.

Also, the ‘add to cart’ code should use a post method form, not a get method link. This is so that when a search engine indexes your site, it doesn’t cause all the add to cart links to be visited.

Sponsor our Newsletter | Privacy Policy | Terms of Service