-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_to_cart.php
29 lines (26 loc) · 1.07 KB
/
add_to_cart.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
session_start();
require 'config.php'; // Database connection
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$user_id = $_SESSION['user_id']; // User must be logged in
$food_id = $_POST['food_id'];
$food_name = $_POST['food_name'];
$quantity = (int)$_POST['quantity'];
$price = (float)$_POST['price'];
// Check for existing cart item
$stmt = $conn->prepare("SELECT * FROM cart WHERE user_id = ? AND food_id = ?");
$stmt->execute([$user_id, $food_id]);
if ($stmt->num_rows() > 0) {
// Update quantity if item already exists
$conn->prepare("UPDATE cart SET quantity = quantity + ? WHERE user_id = ? AND food_id = ?")
->execute([$quantity, $user_id, $food_id]);
} else {
// Insert a new cart item
$conn->prepare("INSERT INTO cart (user_id, food_id, food_name, quantity, price)
VALUES (?, ?, ?, ?, ?)")
->execute([$user_id, $food_id, $food_name, $quantity, $price]);
}
header("Location: cart.php"); // Redirect to cart
exit();
}
?>