Implementazione gestione prodotti in pannello admin

This commit is contained in:
2026-04-10 13:43:07 +02:00
parent d6a64fd459
commit 83adb359f8
+110 -2
View File
@@ -22,6 +22,63 @@ if ($role !== 'admin') {
http_response_code(403);
die("Accesso negato. Solo gli amministratori possono accedere a questa sezione.");
}
// Gestione aggiornamenti
if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['save'])) {
$product_id = $_POST['product_id'];
$name = $_POST['name'];
$description = $_POST['description'];
$category_id = $_POST['category_id'];
$price = $_POST['price'];
$stock = $_POST['stock'];
// Gestione immagine
$image_path = $_POST['current_image'];
if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) {
$target_dir = "../img/";
$target_file = $target_dir . basename($_FILES["image"]["name"]);
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
// Controlli base
if (in_array($imageFileType, ['jpg', 'png', 'jpeg', 'gif'])) {
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
$image_path = "/img/" . basename($_FILES["image"]["name"]);
}
}
}
// Update DB
$sql_update = "UPDATE Products SET ProductName = ?, Description = ?, CategoryID = ?, Price = ?, StockQuantity = ?, ImagePath = ? WHERE ProductID = ?";
$stmt_update = $conn->prepare($sql_update);
$stmt_update->bind_param("ssidisi", $name, $description, $category_id, $price, $stock, $image_path, $product_id);
$stmt_update->execute();
$stmt_update->close();
// Redirect
header("Location: manageProducts.php");
exit();
}
// Ottieni categorie
$categories = [];
$sql_cat = "SELECT CategoryID, Name FROM Categories";
$stmt_cat = $conn->prepare($sql_cat);
$stmt_cat->execute();
$result_cat = $stmt_cat->get_result();
while ($row = $result_cat->fetch_assoc()) {
$categories[] = $row;
}
$stmt_cat->close();
// Ottieni prodotti
$products = [];
$sql_prod = "SELECT ProductID, ProductName, Description, Price, StockQuantity, ImagePath, CategoryID FROM Products";
$stmt_prod = $conn->prepare($sql_prod);
$stmt_prod->execute();
$result_prod = $stmt_prod->get_result();
while ($row = $result_prod->fetch_assoc()) {
$products[] = $row;
}
$stmt_prod->close();
?>
<!DOCTYPE html>
@@ -32,6 +89,13 @@ if ($role !== 'admin') {
<title>Gestione Prodotti - TechStore</title>
<link rel="stylesheet" href="/assets/style.css">
<script src="/assets/js/cart.js"></script>
<script>
function adjustStock(input, delta) {
var value = parseInt(input.value) + delta;
if (value < 0) value = 0;
input.value = value;
}
</script>
</head>
<body>
<header style="display: flex; justify-content: space-between; align-items: center; padding: 10px; background-color: #f2f2f2; border-bottom: 1px solid #ddd;">
@@ -60,8 +124,52 @@ if ($role !== 'admin') {
</ul>
</div>
<div class="my-container">
<h1>Gestione prodotti</h1>
<p>WIP</p>
<h1>Gestione Prodotti</h1>
<table class="product-table">
<thead>
<tr>
<th>ID</th>
<th>Nome</th>
<th>Descrizione</th>
<th>Categoria</th>
<th>Prezzo</th>
<th>Stock</th>
<th>Immagine</th>
<th>Azioni</th>
</tr>
</thead>
<tbody>
<?php foreach ($products as $product): ?>
<tr>
<form action="" method="post" enctype="multipart/form-data">
<td><?php echo $product['ProductID']; ?><input type="hidden" name="product_id" value="<?php echo $product['ProductID']; ?>"></td>
<td><input type="text" name="name" value="<?php echo htmlspecialchars($product['ProductName']); ?>" required></td>
<td><textarea name="description" required><?php echo htmlspecialchars($product['Description']); ?></textarea></td>
<td>
<select name="category_id" required>
<?php foreach ($categories as $cat): ?>
<option value="<?php echo $cat['CategoryID']; ?>" <?php if ($cat['CategoryID'] == $product['CategoryID']) echo 'selected'; ?>><?php echo htmlspecialchars($cat['Name']); ?></option>
<?php endforeach; ?>
</select>
</td>
<td><input type="number" name="price" step="0.01" value="<?php echo $product['Price']; ?>" required></td>
<td>
<div style="display: flex; align-items: center;">
<button type="button" onclick="adjustStock(this.nextElementSibling, -1)">-</button>
<input type="number" name="stock" min="0" value="<?php echo $product['StockQuantity']; ?>" required style="width: 60px; text-align: center;">
<button type="button" onclick="adjustStock(this.previousElementSibling, 1)">+</button>
</div>
</td>
<td>
<img src="<?php echo htmlspecialchars($product['ImagePath']); ?>" alt="Immagine" style="max-width: 100px;"><br>
<input type="file" name="image" accept="image/*">
<input type="hidden" name="current_image" value="<?php echo htmlspecialchars($product['ImagePath']); ?>"></td>
<td><button type="submit" name="save">Salva</button></td>
</form>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<?php else: ?>