<?php
session_start();
require_once('../connection.php');


if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $id = $_POST['product_id'];
    $qty = (int)$_POST['quantity'];

    //create the array
    if (!isset($_SESSION['cart'])) {
        $_SESSION['cart'] = [];
    }

    //add to quantity if already exists
    if (isset($_SESSION['cart'][$id])) {
        $_SESSION['cart'][$id] += $qty;
    } else {
        //add to cart otherwise
        $_SESSION['cart'][$id] = $qty;
    }

    header("Location: " . $_SERVER['HTTP_REFERER']);
    exit;
}