0, 'title' => '', 'subtitle' => '', 'link' => '', 'image' => '', 'sort_order' => 0, 'status' => 1]; if ($id) { $stmt = $db->prepare("SELECT * FROM banners WHERE id = ?"); $stmt->execute([$id]); $banner = $stmt->fetch(PDO::FETCH_ASSOC); if (!$banner) { admin_set_flash('error', 'Not found'); header('Location: ' . ADMIN_PATH . 'pages/banners.php'); exit; } $page_title = 'Edit Banner'; } if ($_SERVER['REQUEST_METHOD'] == 'POST' && verify_csrf($_POST[CSRF_TOKEN_NAME] ?? '')) { $title = trim($_POST['title']); $subtitle = trim($_POST['subtitle'] ?? ''); $link = trim($_POST['link'] ?? ''); $sort_order = intval($_POST['sort_order'] ?? 0); $status = intval($_POST['status']); $image = $banner['image'] ?? ''; if (isset($_FILES['image']) && $_FILES['image']['error'] == 0) { $new_image = upload_image($_FILES['image'], 'banners', $title); if ($new_image) $image = $new_image; } if (!$image && !$id) { $error = 'Banner image is required'; } else { try { if ($id) { $stmt = $db->prepare("UPDATE banners SET title=?, subtitle=?, link=?, image=?, sort_order=?, status=? WHERE id=?"); $stmt->execute([$title, $subtitle, $link, $image, $sort_order, $status, $id]); admin_set_flash('success', 'Banner updated'); } else { $stmt = $db->prepare("INSERT INTO banners (title, subtitle, link, image, sort_order, status) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->execute([$title, $subtitle, $link, $image, $sort_order, $status]); admin_set_flash('success', 'Banner added'); } header('Location: ' . ADMIN_PATH . 'pages/banners.php'); exit; } catch (Exception $e) { $error = 'Failed: ' . $e->getMessage(); } } } ?>