diff --git a/admin.php b/admin.php new file mode 100644 index 0000000..1a1cf7c --- /dev/null +++ b/admin.php @@ -0,0 +1,105 @@ +prepare("DELETE FROM posts WHERE id = ?"); + $stmt->execute([$_POST['delete_id']]); + header("Location: admin.php?deleted=true"); + exit; + } catch (PDOException $e) { + error_log("DB Error: " . $e->getMessage()); + // In a real app, you'd have a more robust error handling system + die("Error deleting post. Check logs for details."); + } +} + +$posts = []; +try { + $pdo = db(); + $stmt = $pdo->query("SELECT id, title, created_at FROM posts ORDER BY created_at DESC"); + $posts = $stmt->fetchAll(); +} catch (PDOException $e) { + error_log("DB Error: " . $e->getMessage()); +} +?> + + + + + + Admin - Manage Posts + + + + + + + + +
+
+

Manage Posts

+ Create New Post +
+ + +
Post deleted successfully.
+ + +
Post saved successfully.
+ + + +
+

No posts found.

+ Create your first post +
+ +
+
+ + + + + + + + + + + + + + + + + +
TitleCreated AtActions
+ Edit +
+ + +
+
+
+
+ +
+ + + + + + diff --git a/editor.php b/editor.php new file mode 100644 index 0000000..65eb820 --- /dev/null +++ b/editor.php @@ -0,0 +1,140 @@ + null, + 'title' => '', + 'content' => '', + 'excerpt' => '', + 'image_url' => '' +]; +$pageTitle = 'Create New Post'; +$action = 'editor.php'; + +// Edit mode +if (isset($_GET['id'])) { + try { + $pdo = db(); + $stmt = $pdo->prepare("SELECT * FROM posts WHERE id = ?"); + $stmt->execute([$_GET['id']]); + $post = $stmt->fetch(); + if (!$post) { + // Post not found, redirect or show error + header("Location: admin.php?error=notfound"); + exit; + } + $pageTitle = 'Edit Post'; + $action = 'editor.php?id=' . $_GET['id']; + } catch (PDOException $e) { + error_log("DB Error: " . $e->getMessage()); + die("Error fetching post. Check logs."); + } +} + +// Handle form submission +if ($_SERVER['REQUEST_METHOD'] === 'POST') { + $title = $_POST['title'] ?? ''; + $content = $_POST['content'] ?? ''; + $excerpt = $_POST['excerpt'] ?? ''; + $imageUrl = $_POST['image_url'] ?? ''; + $id = $_POST['id'] ?? null; + + // Basic validation + if (empty($title) || empty($content)) { + $error = "Title and Content are required."; + } else { + try { + $pdo = db(); + if ($id) { + // Update + $stmt = $pdo->prepare("UPDATE posts SET title = ?, content = ?, excerpt = ?, image_url = ? WHERE id = ?"); + $stmt->execute([$title, $content, $excerpt, $imageUrl, $id]); + } else { + // Insert + $stmt = $pdo->prepare("INSERT INTO posts (title, content, excerpt, image_url) VALUES (?, ?, ?, ?)"); + $stmt->execute([$title, $content, $excerpt, $imageUrl]); + } + header("Location: admin.php?saved=true"); + exit; + } catch (PDOException $e) { + error_log("DB Error: " . $e->getMessage()); + $error = "Error saving post. Check logs for details."; + } + } +} +?> + + + + + + <?php echo $pageTitle; ?> + + + + + + + + +
+
+
+

+ + +
+ + +
+
+
+ + +
+ + +
+ +
+ + +
+ +
+ + +
A short summary of the post, shown on the main blog page.
+
+ +
+ + +
URL for the post's main image. A new random placeholder is generated for you.
+
+ +
+ Cancel + +
+
+
+
+
+
+
+ + + + + + diff --git a/index.php b/index.php index 7b6f570..410a073 100644 --- a/index.php +++ b/index.php @@ -71,7 +71,7 @@ try {