-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_from_cart.php
41 lines (34 loc) · 1.23 KB
/
remove_from_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
30
31
32
33
34
35
36
37
38
39
40
41
<?php
// Ensure that there is no whitespace or output before session_start()
session_start();
// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
// Redirect to login page if user is not logged in
header("Location: login.php");
exit();
}
// Check if item_id is submitted via POST
if (isset($_POST['item_id'])) {
// Sanitize the item_id to prevent SQL injection (assuming item_id is an integer)
$item_id = intval($_POST['item_id']);
// Establish a database connection (assuming you've already included database credentials)
require_once "config2.php";
// Retrieve user_id from session
$user_id = $_SESSION['user_id'];
// Prepare and execute SQL query to remove item from cart
$sql = "DELETE FROM cart_items WHERE id = $item_id AND user_id = $user_id";
if ($conn->query($sql) === TRUE) {
// Item successfully removed from cart
header("Location: cart.php"); // Redirect back to cart page
exit();
} else {
// Error occurred while removing item
echo "Error: " . $conn->error;
}
// Close the database connection
$conn->close();
} else {
// Handle invalid requests
echo "Invalid request";
}
?>