-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.php
More file actions
80 lines (68 loc) · 2.71 KB
/
action.php
File metadata and controls
80 lines (68 loc) · 2.71 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
include("config/constant.php");
// Check if user_id is set in the session
if (!isset($_SESSION['user_id'])) {
die('User ID is not set in the session. Please log in.');
}
$user_id = $_SESSION['user_id'];
if (isset($_POST['pid'])) {
$pid = $_POST['pid'];
$ptitle = $_POST['ptitle'];
$pPrice = $_POST['pPrice'];
$pimage = $_POST['pimage'];
$pcode = $_POST['pcode'];
$pqty = 1;
// Prepare the SQL statement to check for existing product code
$stmt = $conn->prepare("SELECT product_code FROM cart WHERE product_code=? AND user_id=?");
$stmt->bind_param("si", $pcode, $user_id);
// Execute the statement
if ($stmt->execute()) {
$res = $stmt->get_result();
$r = $res->fetch_assoc();
// Check if $r is not null and contains the product_code
if ($r && isset($r['product_code'])) {
$code = $r['product_code'];
} else {
$code = null;
}
// If the product code is not found, insert the new item
if (!$code) {
$query = $conn->prepare("INSERT INTO cart(food_name, price, image_name, qty, total_price, product_code, user_id) VALUES (?, ?, ?, ?, ?, ?, ?)");
$query->bind_param("sssissi", $ptitle, $pPrice, $pimage, $pqty, $pPrice, $pcode, $user_id);
if ($query->execute()) {
echo '<strong>Item Added to your cart!</strong>';
} else {
echo 'Error adding item to cart: ' . $query->error;
}
} else {
echo '<strong>Item Already Added to your cart!</strong>';
}
} else {
echo 'Error executing statement: ' . $stmt->error;
}
}
if (isset($_GET['cartItem']) && $_GET['cartItem'] == 'cart_item') {
// Prepare the SQL statement to count items in the cart for the logged-in user
$stmt = $conn->prepare("SELECT COUNT(*) FROM cart WHERE user_id = ?");
$stmt->bind_param("i", $_SESSION['user_id']); // Bind the user_id parameter
// Execute the statement
if ($stmt->execute()) {
$stmt->bind_result($itemCount); // Bind the result to a variable
$stmt->fetch(); // Fetch the result
echo $itemCount; // Output the number of items in the cart
} else {
echo 'Error executing statement: ' . $stmt->error; // Handle execution error
}
// Close the statement
$stmt->close();
}
if(isset($_GET['remove'])){
$id = $_GET['remove'];
$stmt = $conn-> prepare("DELETE FROM cart where id =?");
$stmt->bind_param("i",$id);
$stmt->execute();
$_SESSION['showAlert']=['block'];
$_SESSION['message']='Item remove from the cart';
header('location:cart.php');
}
?>