prepare("SELECT COUNT(*) FROM categories WHERE slug = ?"); $stmt_check->execute([$slug]); if ($stmt_check->fetchColumn() > 0) { $error = 'A category with this slug already exists. Please choose a unique slug.'; } else { $image_filename = ''; // Handle Image File Upload 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) { // 5MB cap $error = 'Image file size is too large. Keep uploads under 5 megabytes.'; } else { // Create unique target filename $image_filename = 'cat_' . 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 uploaded image. Check directory write permissions.'; $image_filename = ''; } } } if (empty($error)) { // Insert category $stmt_insert = $pdo->prepare("INSERT INTO categories (name, image, slug) VALUES (?, ?, ?)"); $stmt_insert->execute([$name, $image_filename, $slug]); $success = 'Category added successfully! Redirecting back...'; echo ""; } } } catch (Exception $e) { $error = 'Database transaction failed: ' . $e->getMessage(); } } } ?>

Create Category

Establish a new product classification with customized assets.

Only alphanumeric characters and hyphens allowed.
Select a premium 3:4 portrait or high-resolution cover photo. JPG, PNG, WEBP only. Max 5MB.
Image Asset Preview: Preview
Cancel