-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_cart.php
More file actions
133 lines (120 loc) · 4.38 KB
/
Copy pathuser_cart.php
File metadata and controls
133 lines (120 loc) · 4.38 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
session_start();
// Ensure the user is logged in and of type 'user'
if (!isset($_SESSION["email"]) || $_SESSION["user_type"] != "user") {
header("Location: login.php");
exit();
}
// Database connection
$conn = new mysqli("localhost", "root", "", "dbms");
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Get user_id from session
$user_id = intval($_SESSION["user_id"]);
// Fetch the books in the user's cart
$cart_sql = "
SELECT cart.cart_id, books.book_id, books.title, books.author, books.price
FROM cart
JOIN books ON cart.book_id = books.book_id
WHERE cart.user_id = ?";
$cart_stmt = $conn->prepare($cart_sql);
$cart_stmt->bind_param("i", $_SESSION["user_id"]); // binding the session user_id securely
$cart_stmt->execute();
$cart_result = $cart_stmt->get_result();
$cart_stmt->close();
// Calculate total price
$total_price = 0;
$cart_rows = []; // Store rows for display
while ($row = $cart_result->fetch_assoc()) {
$total_price += $row["price"];
$cart_rows[] = $row;
}
// Handle book removal from cart
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST["cart_id"])) {
$cart_id = intval($_POST["cart_id"]);
$book_id = intval($_POST["book_id"]);
// Delete the book from the cart
$delete_sql = "DELETE FROM cart WHERE cart_id = ? AND user_id = ?";
$delete_stmt = $conn->prepare($delete_sql);
$delete_stmt->bind_param("ii", $cart_id, $user_id);
// Execute delete statement and check for success
if ($delete_stmt->execute()) {
// Refresh the page to show updated cart
header("Location: user_cart.php");
exit();
} else {
echo "Failed to remove book from cart.";
}
$delete_stmt->close();
}
?>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="styles.css">
<title>Your Cart</title>
</head>
<body>
<!-- Sidebar for navigation -->
<div class="sidebar">
<h2 class="menu" style="color:#3BABF6">User Menu</h2>
<a href="user_dashboard.php">Dashboard</a>
<a href="books_user.php">Books</a>
<a href="user_orders.php">My Orders</a>
<form method="POST" action="user_dashboard.php">
<button type="submit" name="signout" class="button signout" style="margin: 10px 0; width: 100%;">Sign Out</button>
</form>
</div>
<!-- Main content -->
<div class="container">
<h1>Your Cart</h1>
<?php if (count($cart_rows) > 0): ?>
<table class="table">
<thead>
<tr>
<th>Book ID</th>
<th>Title</th>
<th>Author</th>
<th>Price</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php foreach ($cart_rows as $row): ?>
<tr>
<td><?php echo htmlspecialchars(
$row["book_id"]
); ?></td>
<td><?php echo htmlspecialchars(
$row["title"]
); ?></td>
<td><?php echo htmlspecialchars(
$row["author"]
); ?></td>
<td><?php echo htmlspecialchars(
$row["price"]
); ?></td>
<td>
<form method="POST" action="user_cart.php">
<input type="hidden" name="cart_id" value="<?php echo $row[
"cart_id"
]; ?>">
<input type="hidden" name="book_id" value="<?php echo $row[
"book_id"
]; ?>">
<button type="submit" class="button">Remove</button>
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>Your cart is empty.</p>
<?php endif; ?>
</div>
</body>
</html>
<?php $conn->close(); // Close the database connection ?>