-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit_topic.php
More file actions
103 lines (86 loc) · 3.87 KB
/
edit_topic.php
File metadata and controls
103 lines (86 loc) · 3.87 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
session_start();
try {
// Підключення до бази даних
$pdo = new PDO('sqlite:forum.db');
// Перевірка чи користувач має права адміна
$stmt = $pdo->prepare("SELECT admin FROM users WHERE username = :username");
$stmt->bindParam(':username', $_SESSION['username']);
$stmt->execute();
$user = $stmt->fetch();
if (!$user || $user['admin'] != 1) {
echo "У вас немає прав адміністратора.";
exit();
}
// Редагування теми
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['edit_topic'])) {
$topic_id = $_POST['topic_id'];
$title = $_POST['title'];
$stmt = $pdo->prepare("UPDATE topics SET title = :title WHERE id = :topic_id");
$stmt->bindParam(':title', $title, PDO::PARAM_STR);
$stmt->bindParam(':topic_id', $topic_id, PDO::PARAM_INT);
$stmt->execute();
header("Location: admin.php");
exit();
}
// Видалення теми та пов'язаних дописів
if (isset($_GET['action']) && $_GET['action'] === 'delete_post' && isset($_GET['post_id'])) {
$post_id = $_GET['post_id'];
$stmt = $pdo->prepare("DELETE FROM posts WHERE id = :post_id");
$stmt->bindParam(':post_id', $post_id, PDO::PARAM_INT);
$stmt->execute();
header("Location: edit_topic.php?topic_id=" . $_GET['topic_id']);
exit();
}
// Отримання інформації про тему для редагування
if (isset($_GET['topic_id'])) {
$topic_id = $_GET['topic_id'];
$stmt = $pdo->prepare("SELECT * FROM topics WHERE id = :topic_id");
$stmt->bindParam(':topic_id', $topic_id, PDO::PARAM_INT);
$stmt->execute();
$topic = $stmt->fetch();
// Отримання дописів для даної теми
$stmtPosts = $pdo->prepare("SELECT * FROM posts WHERE topic_id = :topic_id");
$stmtPosts->bindParam(':topic_id', $topic_id, PDO::PARAM_INT);
$stmtPosts->execute();
$posts = $stmtPosts->fetchAll();
}
} catch (PDOException $e) {
echo 'Помилка підключення до бази даних: ' . $e->getMessage();
exit();
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Редагування теми</title>
<link rel="stylesheet" href="style.css">
<link href="https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;700&display=swap" rel="stylesheet">
</head>
<body>
<h1>Редагування теми</h1>
<!-- Форма для редагування теми -->
<div class="topic">
<form action="edit_topic.php" method="post">
<input type="hidden" name="topic_id" value="<?= $topic_id ?>">
<input class="input-field" type="text" name="title" value="<?= htmlspecialchars($topic['title']) ?>" required>
<input class="submit-button" type="submit" name="edit_topic" value="Зберегти зміни">
</form>
</div>
<!-- Список дописів -->
<div class="posts">
<h2 style="display:inline-block; margin-left:20px">Дописи:</h2>
<ul>
<?php foreach ($posts as $post): ?>
<li>
<?= '<div class="post-content">' . nl2br(htmlspecialchars($post['content'])) . '</div>' ?>
<div>
<a href="edit_post.php?action=edit&post_id=<?= $post['id'] ?>">Редагувати</a>
<a href="edit_topic.php?action=delete_post&post_id=<?= $post['id'] ?>&topic_id=<?= $topic_id ?>" onclick="return confirm('Ви впевнені, що хочете видалити цей допис?')">Видалити</a>
</div>
</li>
<?php endforeach; ?>
</ul>
</div>
</body>
</html>