-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_to_cart.php
More file actions
33 lines (28 loc) · 829 Bytes
/
Copy pathadd_to_cart.php
File metadata and controls
33 lines (28 loc) · 829 Bytes
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
30
31
32
33
<?php
include 'db_connect.php';
session_start();
if(!isset($_SESSION['user_id'])) {
header("Location: login.php");
exit();
}
if(isset($_GET['id'])) {
$product_id = (int)$_GET['id'];
$user_id = $_SESSION['user_id'];
// Check if already in cart
$check = mysqli_query($conn,
"SELECT * FROM cart WHERE user_id=$user_id AND product_id=$product_id");
if(mysqli_num_rows($check) > 0) {
// Increase quantity
mysqli_query($conn,
"UPDATE cart SET quantity = quantity + 1
WHERE user_id=$user_id AND product_id=$product_id");
} else {
// Add new item
mysqli_query($conn,
"INSERT INTO cart (user_id, product_id, quantity)
VALUES ($user_id, $product_id, 1)");
}
}
header("Location: cart.php");
exit();
?>