prepare('SELECT id, name FROM categories WHERE id = ?'); $stmt->execute([$category_id]); $category = $stmt->fetch(); if (!$category) { header('Location: categories.php'); exit; } } catch (PDOException $e) { $error_message = 'Database error: ' . $e->getMessage(); } if ($_SERVER['REQUEST_METHOD'] === 'POST') { $name = $_POST['name'] ?? ''; if (empty($name)) { $error_message = 'Please fill in the category name.'; } else { try { $pdo = db(); $stmt = $pdo->prepare('SELECT id FROM categories WHERE name = ? AND id != ?'); $stmt->execute([$name, $category_id]); if ($stmt->fetch()) { $error_message = 'A category with this name already exists.'; } else { $sql = "UPDATE categories SET name = ? WHERE id = ?"; $stmt = $pdo->prepare($sql); $stmt->execute([$name, $category_id]); header("Location: categories.php?success=category_updated"); exit; } } catch (PDOException $e) { $error_message = 'Database error: ' . $e->getMessage(); } } } ?>