0) { // Check if product exists and get details try { $pdo = db(); $stmt = $pdo->prepare("SELECT name, price, image_url FROM products WHERE id = ?"); $stmt->execute([$product_id]); $product = $stmt->fetch(PDO::FETCH_ASSOC); if ($product) { // If item already in cart, update quantity if (isset($_SESSION['cart'][$cart_item_id])) { $_SESSION['cart'][$cart_item_id]['quantity'] += $quantity; } else { // Otherwise, add new item $_SESSION['cart'][$cart_item_id] = [ 'product_id' => $product_id, 'name' => $product['name'], 'price' => $product['price'], 'image_url' => $product['image_url'], 'quantity' => $quantity, 'color' => $color ]; } } } catch (PDOException $e) { // Log error, maybe set a session error message to display in cart error_log("Cart Add Error: " . $e->getMessage()); } } break; case 'update': if ($quantity > 0) { if (isset($_SESSION['cart'][$cart_item_id])) { $_SESSION['cart'][$cart_item_id]['quantity'] = $quantity; } } else { // If quantity is 0 or less, remove the item unset($_SESSION['cart'][$cart_item_id]); } break; case 'remove': if (isset($_SESSION['cart'][$cart_item_id])) { unset($_SESSION['cart'][$cart_item_id]); } break; } // Redirect back to the cart page to show changes header('Location: cart.php'); exit;