diff --git a/src/admin/manageProducts.php b/src/admin/manageProducts.php index 75a64e7..b886971 100644 --- a/src/admin/manageProducts.php +++ b/src/admin/manageProducts.php @@ -59,6 +59,37 @@ if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['save'])) { exit(); } +// Gestione aggiunta nuovo prodotto +if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['add'])) { + $name = $_POST['name']; + $description = $_POST['description']; + $category_id = $_POST['category_id']; + $price = $_POST['price']; + $stock = $_POST['stock']; + $image_path = ''; + + if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) { + $target_dir = "../img/"; + $image_name = basename($_FILES["image"]["name"]); + $target_file = $target_dir . $image_name; + $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION)); + if (in_array($imageFileType, ['jpg', 'png', 'jpeg', 'gif'])) { + if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) { + $image_path = $image_name; + } + } + } + + $sql_insert = "INSERT INTO Products (ProductName, Description, CategoryID, Price, StockQuantity, ImagePath) VALUES (?, ?, ?, ?, ?, ?)"; + $stmt_insert = $conn->prepare($sql_insert); + $stmt_insert->bind_param("ssidis", $name, $description, $category_id, $price, $stock, $image_path); + $stmt_insert->execute(); + $stmt_insert->close(); + + header("Location: manageProducts.php"); + exit(); +} + // Ottieni categorie $categories = []; $sql_cat = "SELECT CategoryID, Name FROM Categories"; @@ -91,11 +122,20 @@ $stmt_prod->close();
@@ -126,6 +166,41 @@ $stmt_prod->close();