71 lines
2.7 KiB
PHP
71 lines
2.7 KiB
PHP
<?php
|
|
require_once 'header.php';
|
|
require_once 'db/config.php';
|
|
|
|
// Check if user is logged in and is an admin
|
|
if (!isset($_SESSION['user_id']) || $_SESSION['role'] !== 'admin') {
|
|
header("Location: profile.php");
|
|
exit;
|
|
}
|
|
|
|
$pdo = db();
|
|
|
|
// Handle product deletion
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['delete_product'])) {
|
|
$product_id = $_POST['product_id'];
|
|
$stmt = $pdo->prepare("DELETE FROM products WHERE id = ?");
|
|
$stmt->execute([$product_id]);
|
|
header("Location: admin_products.php");
|
|
exit;
|
|
}
|
|
|
|
$stmt = $pdo->query("SELECT * FROM products ORDER BY created_at DESC");
|
|
$products = $stmt->fetchAll();
|
|
?>
|
|
|
|
<header class="hero text-center">
|
|
<div class="container">
|
|
<h1 class="display-4">Manage Products</h1>
|
|
</div>
|
|
</header>
|
|
|
|
<main class="container my-5">
|
|
<div class="text-end mb-3">
|
|
<a href="admin_product_edit.php" class="btn btn-primary">Add New Product</a>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-body">
|
|
<table class="table table-striped">
|
|
<thead>
|
|
<tr>
|
|
<th>Image</th>
|
|
<th>Name</th>
|
|
<th>Price</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php foreach ($products as $product): ?>
|
|
<tr>
|
|
<td>
|
|
<img src="<?php echo htmlspecialchars($product['image'] ? $product['image'] : 'https://via.placeholder.com/100x100'); ?>" alt="<?php echo htmlspecialchars($product['name']); ?>" style="width: 100px; height: 100px; object-fit: cover;">
|
|
</td>
|
|
<td><?php echo htmlspecialchars($product['name']); ?></td>
|
|
<td>$<?php echo htmlspecialchars(number_format($product['price'], 2)); ?></td>
|
|
<td>
|
|
<a href="admin_product_edit.php?id=<?php echo $product['id']; ?>" class="btn btn-sm btn-primary">Edit</a>
|
|
<form action="admin_products.php" method="POST" class="d-inline" onsubmit="return confirm('Are you sure you want to delete this product?');">
|
|
<input type="hidden" name="product_id" value="<?php echo $product['id']; ?>">
|
|
<button type="submit" name="delete_product" class="btn btn-sm btn-danger">Delete</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</main>
|
|
|
|
<?php require_once 'footer.php'; ?>
|