I have a little shop and what I want to do is update the quantity of my session after I changed the value in the input field.
cart.php:
if(count($_SESSION['cart_items'])>0){
$product_ids = "";
foreach($_SESSION['cart_items'] as $product_id=>$value){
$product_ids = $product_ids . $product_id . ",";
}
$product_ids = rtrim($product_ids, ',');
$query = "SELECT product_id, name FROM products WHERE product_id IN ({$product_ids}) ORDER BY name";
$stmt = $con->prepare( $query );
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$quan = $_SESSION['cart_items'][$product_id];
echo "{$name}";
echo "<input type='number' value='{$quan}'>";
}
echo "<form action='checkout.php' method='get'>
<button> Checkout</button>
</form>";
}
So when I click on the checkout button, on checkout.php there should be the right quantity for each product.
checkout.php:
$query = "SELECT product_id, name FROM products WHERE product_id IN ({$product_ids}) ORDER BY name";
$stmt = $con->prepare( $query );
$stmt->execute();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
extract($row);
$quan = $_SESSION['cart_items'][$product_id];
echo "{$name}";
echo "{$quan}";
}