Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sorry this has been bugging me for a while now and I still don't understand how to fix this. someone told me
NB: You don't appear to be storing the quantity anywhere on the cart. If the user updates the quantity of "pants" to 2, and then updates the quantity of "shoes" to 2, the quantity of "pants" will be reset to 1.


and now I'm just very confused because I thought that you could get all the information from a form through a post request. I have been trying to find out how to fix this but I can't get any further. so I wanna ask it like this
how do I store the quantity from my users when they submit a form. and how will the form remember the quantity

now situation:
only updates the last option so if you have a cart like this:

item  quantity  price
pants    1       5.00
hat      1       4.00
shoes    1       40.00
----------------------
TOTAL            49.00


and you want to have 2 pants and 2 hats then it will do this

item  quantity  price
pants    1       5.00
hat      1       8.00
shoes    1       40.00
----------------------
TOTAL            53.00


a. it won't remember the quantity the user put in and just set them to one and it won't remember the quantity field and just automatically set that to one. that's where I have to store quantity so that it will remember that I guess but how do i do this??
<?php 
session_start();
error_reporting(E_ALL);
ini_set('display_errrors', '1');
// session_destroy();
?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
        <meta name="description" content="" />
        <meta name="author" content="" />
        <title>Cart</title>
        <!-- Favicon-->
        <link rel="icon" type="image/x-icon" href="assets/favicon.ico" />
        <!-- Bootstrap icons-->
        <link href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.5.0/font/bootstrap-icons.css" rel="stylesheet" />
        <!-- Core theme CSS (includes Bootstrap)-->
        <link href="css/styles.css" rel="stylesheet" />
        <link href="css/stylecart.css" rel="stylesheet" />
        <script src="js/scripts.js" async></script>

    </head>
    <style>
    body {
        font-family: Arial, Helvetica, sans-serif;
        font-size: 20px;
    }

    a{
        text-decoration: none;
        color: white;
    }

    </style>

<body>
<!--navbar-->
<a class="back" href="index.php"> class="bi bi-arrow-left-circle-fill bi-5x"></a>
<?php 
include "config.php";
        ?>
        <div class="text-center" style="font-size: 100px;">🛍</div>
        <h2 class="text-center">Winkelmandje</h2><br>
        <section class="container content-section">
            <!-- <h2 class="section-header">CART</h2> -->
            <div class="cart-row">
                ITEM
                PRICE
                QUANTITY   
                berekening  
                <!-- Verwijderen   -->
            </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){
                        $_SESSION['quantity'] = array();
                         //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();
                        
                         while($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" placeholder="1" min="1" max="100" value="1">';
                                     echo '</div>';
                                  echo '</form>';
                                 echo '</div>';
                            array_push($_SESSION['quantity'], $_POST['quantity']);
                              //session for quantity???'
                              print_r($_SESSION['quantity']);
                                  $quantity = 1;
                                 
                                    if (isset($_POST['quantity']) && !empty($_POST['quantity'])){
                                        $_SESSION['quantity'] = $_POST['quantity'];
                                        
                                        if (isset($_POST['broodnaam']) && !empty($_POST['broodnaam'])){
                                             if ($_POST['broodnaam'] == $row['broodnaam']){
                                                 $quantity = $_POST['quantity'];
                                            }
                                        }
                                    }

                                    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">
                        class="cart-total-title">Total
                         € <?php   echo $sumtotal;?>
                    </div>
                    <br/>
                     
                
                    <button type="button" class="btn btn-danger"><a href="bestellen.php"> Bestellen</a></button>
                   
            
             <br>
           <br />
        </section>
    </body>


What I have tried:

I have been trying to look up that I have to do but all I see is just use $_POST['quantity']; so idk what I'm doing wrong
Posted
Updated 25-Jan-22 2:48am
v2

1 solution

You can get all the information from the form through a POST request. But that information only exists for the duration of that request. As soon as the user submits another form which doesn't include that information, or navigates to another page, that information goes away.

You need to store the quantities alongside the products which the user has added to their cart. Depending on how you have implemented your cart, that would either be stored in the database, or stored in session state.

Typically, you would have at least three sets of data to work with:

Products
ProductID
Name
Price

Carts
CartID

Cart Items
ItemID
CartID
ProductID
Quantity
 
Share this answer
 
Comments
Rebecca2002 25-Jan-22 4:08am    
yeah I was thinking of using a session and did $_SESSION['quantity'] = $_POST['quantity']; but again that shows just the last one that the user enters.
Richard Deeming 25-Jan-22 4:19am    
You can't just store a single number in the session and expect the system to magically work out which cart item it applies to! Let alone remember what the previous values were and which cart items they applied to.

You need to store a quantity against each cart item.
Rebecca2002 25-Jan-22 8:06am    
been trying to figure this out for 3 hours now but still don't get it. do you maybe have a link for storing the quantity against each cart item? I'm so sorry I feel so stupid right now but I just don't get it.
Richard Deeming 25-Jan-22 8:40am    
Are you storing the cart items in the database? If so, you just need to issue an UPDATE command[^] to update the quantity of the item based on the cart ID and product.
Rebecca2002 25-Jan-22 8:47am    
no I don't I actually wanna use sessions unless its more difficult to use them

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