Gitlab@Informatics

Skip to content
Snippets Groups Projects
Commit 3de7a29e authored by 65160260's avatar 65160260
Browse files

Edit cart.php

parent 74ca4d0e
No related branches found
No related tags found
No related merge requests found
...@@ -11,8 +11,8 @@ if (!isset($_SESSION['user_id'])) { ...@@ -11,8 +11,8 @@ if (!isset($_SESSION['user_id'])) {
// ✅ เพิ่มสินค้าเข้า `cart` // ✅ เพิ่มสินค้าเข้า `cart`
if (isset($_GET['add'])) { if (isset($_GET['add'])) {
$stmt = $conn->prepare(" $stmt = $conn->prepare("
INSERT INTO cart (users_id, products_id, qty) INSERT INTO cart (id, users_id, products_id, qty)
VALUES (?, ?, 1) VALUES (NULL, ?, ?, 1)
ON DUPLICATE KEY UPDATE qty = qty + 1 ON DUPLICATE KEY UPDATE qty = qty + 1
"); ");
$stmt->execute([$_SESSION['user_id'], $_GET['add']]); $stmt->execute([$_SESSION['user_id'], $_GET['add']]);
...@@ -20,6 +20,23 @@ if (isset($_GET['add'])) { ...@@ -20,6 +20,23 @@ if (isset($_GET['add'])) {
exit; exit;
} }
// ✅ อัปเดตจำนวนสินค้า
if (isset($_GET['update']) && isset($_GET['qty'])) {
$qty = max(1, intval($_GET['qty'])); // ป้องกันค่าติดลบ
$stmt = $conn->prepare("UPDATE cart SET qty = ? WHERE users_id = ? AND products_id = ?");
$stmt->execute([$qty, $_SESSION['user_id'], $_GET['update']]);
header("Location: cart.php");
exit;
}
// ✅ ลบสินค้าออกจาก `cart`
if (isset($_GET['remove'])) {
$stmt = $conn->prepare("DELETE FROM cart WHERE users_id = ? AND products_id = ?");
$stmt->execute([$_SESSION['user_id'], $_GET['remove']]);
header("Location: cart.php");
exit;
}
// ✅ ดึงข้อมูลจาก `cart` และ `products` // ✅ ดึงข้อมูลจาก `cart` และ `products`
$stmt = $conn->prepare(" $stmt = $conn->prepare("
SELECT cart.*, products.name, products.price SELECT cart.*, products.name, products.price
...@@ -36,8 +53,6 @@ $cart_items = $stmt->fetchAll(PDO::FETCH_ASSOC); ...@@ -36,8 +53,6 @@ $cart_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
<?php if (empty($cart_items)): ?> <?php if (empty($cart_items)): ?>
<p>ไม่มีสินค้าในตะกร้า</p> <p>ไม่มีสินค้าในตะกร้า</p>
<?php else: ?> <?php else: ?>
<table border="1" cellpadding="5" cellspacing="0"> <table border="1" cellpadding="5" cellspacing="0">
<tr> <tr>
<th>User ID</th> <th>User ID</th>
...@@ -61,7 +76,7 @@ $cart_items = $stmt->fetchAll(PDO::FETCH_ASSOC); ...@@ -61,7 +76,7 @@ $cart_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
<td><?= htmlspecialchars($item['name']) ?></td> <td><?= htmlspecialchars($item['name']) ?></td>
<td><?= number_format($item['price'], 2) ?> บาท</td> <td><?= number_format($item['price'], 2) ?> บาท</td>
<td> <td>
<a href="cart.php?update=<?= $item['products_id'] ?>&qty=<?= $item['qty'] - 1 ?>"></a> <a href="cart.php?update=<?= $item['products_id'] ?>&qty=<?= max(1, $item['qty'] - 1) ?>"></a>
<?= $item['qty'] ?> <?= $item['qty'] ?>
<a href="cart.php?update=<?= $item['products_id'] ?>&qty=<?= $item['qty'] + 1 ?>"></a> <a href="cart.php?update=<?= $item['products_id'] ?>&qty=<?= $item['qty'] + 1 ?>"></a>
</td> </td>
...@@ -70,7 +85,7 @@ $cart_items = $stmt->fetchAll(PDO::FETCH_ASSOC); ...@@ -70,7 +85,7 @@ $cart_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
</tr> </tr>
<?php endforeach; ?> <?php endforeach; ?>
<tr> <tr>
<td colspan="4" align="right"><strong>ยอดรวมทั้งหมด:</strong></td> <td colspan="5" align="right"><strong>ยอดรวมทั้งหมด:</strong></td>
<td colspan="2"><strong><?= number_format($total, 2) ?> บาท</strong></td> <td colspan="2"><strong><?= number_format($total, 2) ?> บาท</strong></td>
</tr> </tr>
</table> </table>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment