-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit-post.php
More file actions
executable file
·185 lines (157 loc) · 7.69 KB
/
Copy pathedit-post.php
File metadata and controls
executable file
·185 lines (157 loc) · 7.69 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
$page_title = "Edit Post";
$page_js = "post.js";
include 'includes/header.php';
// Check if user is logged in
if (!is_logged_in()) {
header("Location: login.php");
exit;
}
// Check if post ID is provided
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
header("Location: index.php");
exit;
}
$post_id = (int)$_GET['id'];
$user_id = $_SESSION['user_id'];
// Get post details and check ownership
$post_query = "SELECT * FROM posts WHERE post_id = ? AND user_id = ?";
$stmt = mysqli_prepare($conn, $post_query);
mysqli_stmt_bind_param($stmt, "ii", $post_id, $user_id);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if (mysqli_num_rows($result) == 0) {
header("Location: index.php");
exit;
}
$post = mysqli_fetch_assoc($result);
// Process post update
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = sanitize_input($_POST['title']);
$content = sanitize_input($_POST['content']);
$category = sanitize_input($_POST['category']);
$tags = isset($_POST['tags']) ? sanitize_input($_POST['tags']) : '';
// Validate inputs
if (!empty($title) && !empty($content) && !empty($category)) {
// Update post
$update_query = "UPDATE posts SET title = ?, content = ?, category = ? WHERE post_id = ? AND user_id = ?";
$stmt = mysqli_prepare($conn, $update_query);
mysqli_stmt_bind_param($stmt, "sssii", $title, $content, $category, $post_id, $user_id);
if (mysqli_stmt_execute($stmt)) {
// Delete existing tags
$delete_tags = "DELETE FROM post_tags WHERE post_id = ?";
$stmt = mysqli_prepare($conn, $delete_tags);
mysqli_stmt_bind_param($stmt, "i", $post_id);
mysqli_stmt_execute($stmt);
// Process tags
if (!empty($tags)) {
$tag_list = explode(',', $tags);
// Limit to 5 tags
if (count($tag_list) > 5) {
$tag_list = array_slice($tag_list, 0, 5);
}
foreach ($tag_list as $key => $tag_name) {
$tag_name = trim($tag_name);
if (empty($tag_name)) {
unset($tag_list[$key]);
continue;
}
// Sanitize each tag
$tag_list[$key] = sanitize_input($tag_name);
// Get or create tag
$tag_query = "SELECT tag_id FROM tags WHERE name = ?";
$stmt = mysqli_prepare($conn, $tag_query);
mysqli_stmt_bind_param($stmt, "s", $tag_name);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if (mysqli_num_rows($result) > 0) {
$tag = mysqli_fetch_assoc($result);
$tag_id = $tag['tag_id'];
} else {
$insert_tag = "INSERT INTO tags (name) VALUES (?)";
$stmt = mysqli_prepare($conn, $insert_tag);
mysqli_stmt_bind_param($stmt, "s", $tag_name);
mysqli_stmt_execute($stmt);
$tag_id = mysqli_insert_id($conn);
}
// Link tag to post
$link_tag = "INSERT INTO post_tags (post_id, tag_id) VALUES (?, ?)";
$stmt = mysqli_prepare($conn, $link_tag);
mysqli_stmt_bind_param($stmt, "ii", $post_id, $tag_id);
mysqli_stmt_execute($stmt);
}
}
// Redirect to the post
header("Location: post.php?id=$post_id");
exit;
} else {
$error = "Error updating post: " . mysqli_error($conn);
}
} else {
$error = "All fields are required";
}
}
// Get post tags
$tag_query = "SELECT t.name FROM tags t JOIN post_tags pt ON t.tag_id = pt.tag_id WHERE pt.post_id = ?";
$stmt = mysqli_prepare($conn, $tag_query);
mysqli_stmt_bind_param($stmt, "i", $post_id);
mysqli_stmt_execute($stmt);
$tag_result = mysqli_stmt_get_result($stmt);
$tags = [];
while ($tag = mysqli_fetch_assoc($tag_result)) {
$tags[] = $tag['name'];
}
$tags_string = implode(',', $tags);
?>
<div class="form-container" style="max-width: 800px;">
<h2 class="form-title">Edit Post</h2>
<?php if (isset($error)): ?>
<div class="alert alert-danger"><?php echo $error; ?></div>
<?php endif; ?>
<form id="post-form" method="post" action="">
<div class="form-group">
<label for="title">Title</label>
<input type="text" class="form-control" id="title" name="title" value="<?php echo htmlspecialchars($post['title']); ?>" required>
</div>
<div class="form-group">
<label for="content">Content</label>
<textarea class="form-control" id="content" name="content" rows="10" required><?php echo htmlspecialchars($post['content']); ?></textarea>
</div>
<div class="form-group">
<label for="category">Category</label>
<select class="form-control" id="category" name="category" required>
<option value="">Select a category</option>
<option value="general" <?php if ($post['category'] === 'general') echo 'selected'; ?>>General</option>
<option value="technology" <?php if ($post['category'] === 'technology') echo 'selected'; ?>>Technology</option>
<option value="programming" <?php if ($post['category'] === 'programming') echo 'selected'; ?>>Programming</option>
<option value="science" <?php if ($post['category'] === 'science') echo 'selected'; ?>>Science</option>
<option value="gaming" <?php if ($post['category'] === 'gaming') echo 'selected'; ?>>Gaming</option>
<option value="art" <?php if ($post['category'] === 'art') echo 'selected'; ?>>Art & Design</option>
<option value="music" <?php if ($post['category'] === 'music') echo 'selected'; ?>>Music</option>
<option value="movies" <?php if ($post['category'] === 'movies') echo 'selected'; ?>>Movies & TV</option>
<option value="books" <?php if ($post['category'] === 'books') echo 'selected'; ?>>Books & Literature</option>
<option value="sports" <?php if ($post['category'] === 'sports') echo 'selected'; ?>>Sports</option>
<option value="other" <?php if ($post['category'] === 'other') echo 'selected'; ?>>Other</option>
</select>
</div>
<div class="form-group">
<label for="tag-input">Tags (Max 5)</label>
<input type="text" class="form-control" id="tag-input" placeholder="Type a tag and press Enter or comma">
<div id="tag-list" class="tag-list">
<?php foreach ($tags as $tag): ?>
<div class="tag-item" data-tag="<?php echo htmlspecialchars($tag); ?>">
<span class="tag-name"><?php echo htmlspecialchars($tag); ?></span>
<span class="tag-remove">×</span>
</div>
<?php endforeach; ?>
</div>
<input type="hidden" id="tags" name="tags" value="<?php echo htmlspecialchars($tags_string); ?>">
<small class="form-text" style="color: #666;">Tags help others find your post. Add up to 5 tags.</small>
</div>
<button type="submit" class="btn btn-block">Update Post</button>
<div class="form-footer" style="margin-top: 15px; text-align: center;">
<a href="post.php?id=<?php echo $post_id; ?>">Cancel and go back to post</a>
</div>
</form>
</div>
<?php include 'includes/footer.php'; ?>