diff --git a/src/admin/manageProducts.php b/src/admin/manageProducts.php index c79c661..0270496 100644 --- a/src/admin/manageProducts.php +++ b/src/admin/manageProducts.php @@ -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(); ?> @@ -32,6 +89,13 @@ if ($role !== 'admin') {
WIP
+| ID | +Nome | +Descrizione | +Categoria | +Prezzo | +Stock | +Immagine | +Azioni | +
|---|---|---|---|---|---|---|---|