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 = []; try { $colors = $pdo->query("SELECT * FROM colors ORDER BY name ASC")->fetchAll(); $sizes = $pdo->query("SELECT * FROM sizes ORDER BY id ASC")->fetchAll(); } catch (Exception $e) { $error = 'Failed to load custom attributes: ' . $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 = ''; // Handle Image Upload (Required for new product) 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 $image_filename = 'prod_' . time() . '_' . uniqid() . '.' . $file_ext; $upload_dir = __DIR__ . '/../assets/images/'; if (!is_dir($upload_dir)) { mkdir($upload_dir, 0755, true); } if (!move_uploaded_file($file_tmp, $upload_dir . $image_filename)) { $error = 'Failed to save product image to local storage.'; $image_filename = ''; } } } else { $error = 'A product showcase image upload is required.'; } if (empty($error)) { $pdo->beginTransaction(); // Insert product into database $stmt_insert = $pdo->prepare("INSERT INTO products (name, description, price, category_id, image, stock, is_featured) VALUES (?, ?, ?, ?, ?, ?, ?)"); $stmt_insert->execute([$name, $description, $price, $category_id, $image_filename, $stock, $is_featured]); $product_id = $pdo->lastInsertId(); // Save 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([$product_id, intval($cid)]); } } // Save 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([$product_id, intval($sid)]); } } $pdo->commit(); $success = 'Product introduced successfully! Redirecting back...'; echo ""; } } catch (Exception $e) { if ($pdo->inTransaction()) { $pdo->rollBack(); } $error = 'Database transaction failed: ' . $e->getMessage(); } } } ?>
Design and list a new masterpiece for the catalog collection.