35595-vm/product.php
Flatlogic Bot 89d2171b50 1.0.1
2025-11-09 14:00:35 +00:00

70 lines
2.2 KiB
PHP

<?php
require_once 'header.php';
require_once 'db/config.php';
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
header("Location: shop.php");
exit;
}
$product_id = $_GET['id'];
$pdo = db();
$stmt = $pdo->prepare("SELECT * FROM products WHERE id = ?");
$stmt->execute([$product_id]);
$product = $stmt->fetch();
if (!$product) {
header("Location: shop.php");
exit;
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['add_to_cart'])) {
$quantity = isset($_POST['quantity']) && is_numeric($_POST['quantity']) && $_POST['quantity'] > 0 ? (int)$_POST['quantity'] : 1;
if (!isset($_SESSION['cart'])) {
$_SESSION['cart'] = [];
}
if (isset($_SESSION['cart'][$product_id])) {
$_SESSION['cart'][$product_id] += $quantity;
} else {
$_SESSION['cart'][$product_id] = $quantity;
}
header("Location: cart.php");
exit;
}
?>
<header class="hero text-center">
<div class="container">
<h1 class="display-4"><?php echo htmlspecialchars($product['name']); ?></h1>
</div>
</header>
<main class="container my-5">
<div class="row">
<div class="col-md-6">
<img src="https://via.placeholder.com/600x400" class="img-fluid" alt="<?php echo htmlspecialchars($product['name']); ?>">
</div>
<div class="col-md-6">
<h2><?php echo htmlspecialchars($product['name']); ?></h2>
<p class="lead"><?php echo htmlspecialchars($product['description']); ?></p>
<p class="fs-3 text-primary">$<?php echo htmlspecialchars(number_format($product['price'], 2)); ?></p>
<form action="product.php?id=<?php echo $product_id; ?>" method="POST">
<div class="row">
<div class="col-md-4">
<label for="quantity" class="form-label">Quantity</label>
<input type="number" class="form-control" id="quantity" name="quantity" value="1" min="1">
</div>
</div>
<button type="submit" name="add_to_cart" class="btn btn-primary btn-lg mt-3">Add to Cart</button>
</form>
</div>
</div>
</main>
<?php require_once 'footer.php'; ?>