-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathedit_blogs_backend.php
More file actions
30 lines (25 loc) · 961 Bytes
/
edit_blogs_backend.php
File metadata and controls
30 lines (25 loc) · 961 Bytes
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
<?php
header('Content-Type: application/json');
try {
$db = new PDO("sqlite:blogs.db");
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$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
)");
$action = $_POST['action'] ?? '';
if ($action === 'delete') {
$stmt = $db->prepare("DELETE FROM blogs WHERE id = ?");
$stmt->execute([$_POST['id']]);
echo json_encode(['status' => 'success']);
} else {
// Update
$stmt = $db->prepare("UPDATE blogs SET title = ?, content = ? WHERE id = ?");
$stmt->execute([$_POST['title'], $_POST['content'], $_POST['id']]);
echo json_encode(['status' => 'success']);
}
} catch (Exception $e) {
echo json_encode(['status' => 'error', 'message' => $e->getMessage()]);
}