prepare("SELECT * FROM categories WHERE id = ?"); $stmt->execute([$cat_id]); $category = $stmt->fetch(); if (!$category) { header("Location: categories.php"); exit; } } catch (Exception $e) { $error = 'Error loading category: ' . $e->getMessage(); } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = isset($_POST['name']) ? trim($_POST['name']) : ''; $slug = isset($_POST['slug']) ? trim($_POST['slug']) : ''; // Clean slug structure $slug = preg_replace('/[^a-z0-9\-]/', '', strtolower(str_replace(' ', '-', $slug))); if (empty($name) || empty($slug)) { $error = 'Both Name and Slug are required.'; } else { try { // Check slug uniqueness except for current category $stmt_check = $pdo->prepare("SELECT COUNT(*) FROM categories WHERE slug = ? AND id != ?"); $stmt_check->execute([$slug, $cat_id]); if ($stmt_check->fetchColumn() > 0) { $error = 'A category with this slug already exists. Please choose a unique slug.'; } else { $image_filename = $category['image']; // Default to current image // Handle Image File Upload if uploaded 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 = 'cat_' . time() . '_' . uniqid() . '.' . $file_ext; $upload_dir = __DIR__ . '/../assets/images/'; if (move_uploaded_file($file_tmp, $upload_dir . $new_image_filename)) { // Delete old image if it exists and is custom if (!empty($category['image']) && file_exists($upload_dir . $category['image']) && strpos($category['image'], 'cat_') !== false) { @unlink($upload_dir . $category['image']); } $image_filename = $new_image_filename; } else { $error = 'Failed to save new image.'; } } } if (empty($error)) { // Update category $stmt_update = $pdo->prepare("UPDATE categories SET name = ?, image = ?, slug = ? WHERE id = ?"); $stmt_update->execute([$name, $image_filename, $slug, $cat_id]); $success = 'Category updated successfully! Redirecting back...'; // Reload local state $category['name'] = $name; $category['slug'] = $slug; $category['image'] = $image_filename; echo ""; } } } catch (Exception $e) { $error = 'Database update failed: ' . $e->getMessage(); } } } ?>

Refine Category Asset

Update branding characteristics, names, and covers for "".

Alphanumeric tokens and dashes only.
Leave empty to retain the current image asset. PNG, JPG, WEBP. Max 5MB.
Active Image Asset: Current Image
Newly Selected Image: New Preview
Cancel