-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcart.php
118 lines (85 loc) · 3.61 KB
/
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
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
<?php
$DATABASE = 'shoppingcart';
$USERNAME = 'root';
$PASSWORD = '';
$connection = new PDO('mysql:host=localhost;dbname=' . $DATABASE, $USERNAME, $PASSWORD);
function AddItemToCart($name, $quantity) {
global $connection;
$query = $connection->prepare('INSERT INTO tblitem (itemName, cartID, quantity) VALUES (:itemName, :cartID, :quantity)');
$query->bindParam(':itemName', $name);
$query->bindParam(':cartID', $_GET['id']);
$query->bindParam(':quantity', $quantity);
$query->execute();
header("Location: cart.php?id={$_GET['id']}");
}
function GetCartItems() {
global $connection;
$query = $connection->prepare('SELECT * FROM tblitem WHERE cartID = :cartID');
$query->bindParam(':cartID', $_GET['id']);
$query->execute();
$items = $query->fetchAll();
return $items;
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
$quantity = $_POST['quantity'];
AddItemToCart($name, $quantity);
header("Location: cart.php?id={$_GET['id']}");
}
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
if (isset($_GET['itemId'])) {
$id = $_GET['itemId'];
$query = $connection->prepare('DELETE FROM tblitem WHERE id = :id');
$query->bindParam(':id', $id);
$query->execute();
header("Location: cart.php?id={$_GET['id']}");
}
}
if (!isset($_GET['id']) && $_SERVER['REQUEST_METHOD'] == 'GET') {
echo 'No cart ID provided';
exit();
header('Location: index.php');
}
?>
<html>
<head>
<script src="https://cdn.tailwindcss.com"></script>
<title>Cart</title>
</head>
<body>
<a class="absolute ml-3 mt-3 text-center bg-cyan-500 hover:bg-cyan-600 hover:pointer w-32 rounded-full text-lg font-bold p-2" href="index.php">Back</a>
<h1 class="text-center text-5xl font-bold w-full">Cart</h1>
<div class="flex justify-center mt-10 flex-col mx-32 gap-5">
<form action="cart.php?id=<?php echo $_GET['id']; ?>" method="POST">
<input class="border px-4 py-2" type="text" name="name" placeholder="Item Name">
<input class="border px-4 py-2" type="number" name="quantity" placeholder="Quantity">
<button class="bg-cyan-500 hover:bg-cyan-600 hover:pointer w-32 rounded-full text-lg font-bold p-2" type="submit">Add Item</button>
</form>
</div>
<div class="flex justify-center mt-10 flex-col mx-32 gap-5">
<table>
<thead>
<tr>
<th class="border px-4 py-2">Item</th>
<th class="border px-4 py-2">Quantity</th>
<th class="border px-4 py-2">Action</th>
</tr>
</thead>
<tbody>
<?php
$items = GetCartItems();
foreach ($items as $item) {
echo '<tr>';
echo '<td class="border px-4 py-2">' . $item['itemName'] . '</td>';
echo '<td class="border px-4 py-2">' . $item['quantity'] . '</td>';
echo '<td class="border px-4 py-2">
<a href="cart.php?id=' . $_GET['id'] . '&itemId=' . $item['id'] . '" class="bg-red-500 hover:bg-red-600 hover:pointer w-32 rounded-full text-lg font-bold p-2">Delete</a>
</td>';
echo '</tr>';
}
?>
</tbody>
</table>
</div>
</body>
</html>