Select Git revision
contributing.md
After you've reviewed these contribution guidelines, you'll be all set to
contribute to this project.
cart.php 2.77 KiB
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include 'connect.php';
session_start();
if (!isset($_SESSION['user_id'])) {
die("กรุณาเข้าสู่ระบบก่อน");
}
// ✅ เพิ่มสินค้าเข้า `cart`
if (isset($_GET['add'])) {
$stmt = $conn->prepare("
INSERT INTO cart (users_id, products_id, qty)
VALUES (?, ?, 1)
ON DUPLICATE KEY UPDATE qty = qty + 1
");
$stmt->execute([$_SESSION['user_id'], $_GET['add']]);
header("Location: cart.php");
exit;
}
// ✅ ดึงข้อมูลจาก `cart` และ `products`
$stmt = $conn->prepare("
SELECT cart.*, products.name, products.price
FROM cart
JOIN products ON cart.products_id = products.id
WHERE cart.users_id = ?
");
$stmt->execute([$_SESSION['user_id']]);
$cart_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>
<h2>ตะกร้าสินค้า</h2>
<?php if (empty($cart_items)): ?>
<p>ไม่มีสินค้าในตะกร้า</p>
<?php else: ?>
<table border="1" cellpadding="5" cellspacing="0">
<tr>
<th>User ID</th>
<th>รหัสสินค้า</th>
<th>ชื่อสินค้า</th>
<th>ราคา</th>
<th>จำนวน</th>
<th>รวม</th>
<th>การจัดการ</th>
</tr>
<?php
$total = 0;
foreach ($cart_items as $item):
$subtotal = $item['price'] * $item['qty'];
$total += $subtotal;
?>
<tr>
<td><?= $item['users_id'] ?></td>
<td><?= $item['products_id'] ?></td>
<td><?= htmlspecialchars($item['name']) ?></td>
<td><?= number_format($item['price'], 2) ?> บาท</td>
<td>
<a href="cart.php?update=<?= $item['products_id'] ?>&qty=<?= $item['qty'] - 1 ?>">➖</a>
<?= $item['qty'] ?>
<a href="cart.php?update=<?= $item['products_id'] ?>&qty=<?= $item['qty'] + 1 ?>">➕</a>
</td>
<td><?= number_format($subtotal, 2) ?> บาท</td>
<td><a href="cart.php?remove=<?= $item['products_id'] ?>" onclick="return confirm('ลบสินค้านี้ออกจากตะกร้า?')">ลบ</a></td>
</tr>