query("SELECT id, name FROM categories ORDER BY name ASC"); $categories = $stmt_cats->fetchAll(); } catch (Exception $e) { $error = 'Failed to load categories: ' . $e->getMessage(); } // Fetch active colors and sizes for checkboxes $colors = []; $sizes = []; $assigned_colors = []; $assigned_sizes = []; try { $colors = $pdo->query("SELECT * FROM colors ORDER BY name ASC")->fetchAll(); $sizes = $pdo->query("SELECT * FROM sizes ORDER BY id ASC")->fetchAll(); // Fetch assigned colors for current product $stmt_assigned_colors = $pdo->prepare("SELECT color_id FROM product_colors WHERE product_id = ?"); $stmt_assigned_colors->execute([$prod_id]); $assigned_colors = $stmt_assigned_colors->fetchAll(PDO::FETCH_COLUMN); // Fetch assigned sizes for current product $stmt_assigned_sizes = $pdo->prepare("SELECT size_id FROM product_sizes WHERE product_id = ?"); $stmt_assigned_sizes->execute([$prod_id]); $assigned_sizes = $stmt_assigned_sizes->fetchAll(PDO::FETCH_COLUMN); } catch (Exception $e) { $error = 'Failed to load custom attributes: ' . $e->getMessage(); } // Fetch existing product details try { $stmt = $pdo->prepare("SELECT * FROM products WHERE id = ?"); $stmt->execute([$prod_id]); $product = $stmt->fetch(); if (!$product) { header("Location: products.php"); exit; } } catch (Exception $e) { $error = 'Error loading product: ' . $e->getMessage(); } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = isset($_POST['name']) ? trim($_POST['name']) : ''; $description = isset($_POST['description']) ? trim($_POST['description']) : ''; $price = isset($_POST['price']) ? floatval($_POST['price']) : 0.00; $category_id = isset($_POST['category_id']) ? intval($_POST['category_id']) : 0; $stock = isset($_POST['stock']) ? intval($_POST['stock']) : 0; $is_featured = isset($_POST['is_featured']) ? 1 : 0; $selected_colors = isset($_POST['colors']) ? $_POST['colors'] : []; $selected_sizes = isset($_POST['sizes']) ? $_POST['sizes'] : []; if (empty($name) || empty($description) || $price <= 0 || $category_id <= 0) { $error = 'Please fill out all required fields: Name, Description, Category, and a valid Price.'; } else { try { $image_filename = $product['image']; // Default to current image // Handle Image Upload if selected if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) { $file_tmp = $_FILES['image']['tmp_name']; $file_name = $_FILES['image']['name']; $file_size = $_FILES['image']['size']; $file_ext = strtolower(pathinfo($file_name, PATHINFO_EXTENSION)); $allowed_exts = ['png', 'jpg', 'jpeg', 'webp']; if (!in_array($file_ext, $allowed_exts)) { $error = 'Invalid image type. Only PNG, JPG, JPEG, and WEBP formats are accepted.'; } elseif ($file_size > 5 * 1024 * 1024) { $error = 'Image file size is too large. Keep uploads under 5 megabytes.'; } else { // Create unique target filename $new_image_filename = 'prod_' . time() . '_' . uniqid() . '.' . $file_ext; $upload_dir = __DIR__ . '/../assets/images/'; if (move_uploaded_file($file_tmp, $upload_dir . $new_image_filename)) { // Delete old custom product image if exists if (!empty($product['image']) && file_exists($upload_dir . $product['image']) && strpos($product['image'], 'prod_') !== false) { @unlink($upload_dir . $product['image']); } $image_filename = $new_image_filename; } else { $error = 'Failed to save product image to local storage.'; } } } if (empty($error)) { $pdo->beginTransaction(); // Update product in database $stmt_update = $pdo->prepare("UPDATE products SET name = ?, description = ?, price = ?, category_id = ?, image = ?, stock = ?, is_featured = ? WHERE id = ?"); $stmt_update->execute([$name, $description, $price, $category_id, $image_filename, $stock, $is_featured, $prod_id]); // Clear previous colors and sizes $stmt_clear_colors = $pdo->prepare("DELETE FROM product_colors WHERE product_id = ?"); $stmt_clear_colors->execute([$prod_id]); $stmt_clear_sizes = $pdo->prepare("DELETE FROM product_sizes WHERE product_id = ?"); $stmt_clear_sizes->execute([$prod_id]); // Re-insert selected colors if (!empty($selected_colors)) { $stmt_prod_color = $pdo->prepare("INSERT INTO product_colors (product_id, color_id) VALUES (?, ?)"); foreach ($selected_colors as $cid) { $stmt_prod_color->execute([$prod_id, intval($cid)]); } } // Re-insert selected sizes if (!empty($selected_sizes)) { $stmt_prod_size = $pdo->prepare("INSERT INTO product_sizes (product_id, size_id) VALUES (?, ?)"); foreach ($selected_sizes as $sid) { $stmt_prod_size->execute([$prod_id, intval($sid)]); } } $pdo->commit(); // Refresh local variables for page rendering $assigned_colors = $selected_colors; $assigned_sizes = $selected_sizes; $success = 'Product updated successfully! Redirecting back...'; // Refresh local product details $product['name'] = $name; $product['description'] = $description; $product['price'] = $price; $product['category_id'] = $category_id; $product['image'] = $image_filename; $product['stock'] = $stock; $product['is_featured'] = $is_featured; echo ""; } } catch (Exception $e) { if ($pdo->inTransaction()) { $pdo->rollBack(); } $error = 'Database update transaction failed: ' . $e->getMessage(); } } } ?>
Update classifications, listings, and assets for "".