-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsave_blog.php
More file actions
39 lines (32 loc) · 1.14 KB
/
save_blog.php
File metadata and controls
39 lines (32 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
session_start();
header("Content-Type: application/json");
try {
$db = new PDO("sqlite:blogs.db");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Ensure table exists
$db->exec("CREATE TABLE IF NOT EXISTS blogs (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)");
// Insert new blog
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = trim($_POST['title'] ?? '');
$content = trim($_POST['content'] ?? '');
if ($title === '' || $content === '') {
echo json_encode(["status" => "error", "message" => "Missing title or content"]);
exit();
}
$stmt = $db->prepare("INSERT INTO blogs (title, content) VALUES (:title, :content)");
$stmt->execute([
":title" => $title,
":content" => $content
]);
echo json_encode(["status" => "success", "message" => "Blog saved"]);
exit();
}
} catch (Exception $e) {
echo json_encode(["status" => "error", "message" => $e->getMessage()]);
}