-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreply.php
More file actions
52 lines (46 loc) · 1.67 KB
/
Copy pathreply.php
File metadata and controls
52 lines (46 loc) · 1.67 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
<?php
// Optional separate endpoint for reply submissions
// Created: 2025-08-10 03:54:32 UTC by PRITHVIBO
require_once 'init.php';
require_login();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$post_id = (int)($_POST['post_id'] ?? 0);
$body = trim($_POST['body'] ?? '');
if ($post_id && $body !== '') {
// Verify post exists
$checkStmt = $pdo->prepare("SELECT id FROM posts WHERE id = ?");
$checkStmt->execute([$post_id]);
if ($checkStmt->fetch()) {
// Prefer 'content' column if exists
try {
$cols = $pdo->query("DESCRIBE replies")->fetchAll(PDO::FETCH_COLUMN, 0);
} catch (PDOException $e) {
$cols = [];
}
$textPriority = ['content', 'body', 'reply', 'message', 'text', 'comment'];
$replyCol = null;
foreach ($textPriority as $colName) {
if (in_array($colName, $cols, true)) {
$replyCol = $colName;
break;
}
}
$column = $replyCol ?: 'content';
$stmt = $pdo->prepare("INSERT INTO replies (post_id, user_id, `$column`) VALUES (?, ?, ?)");
if ($stmt->execute([$post_id, current_user()['id'], $body])) {
set_flash('Reply posted successfully!');
} else {
set_flash('Failed to post reply.');
}
} else {
set_flash('Post not found.');
}
redirect('post.php?id=' . $post_id);
} else {
set_flash('Invalid input.');
if ($post_id) {
redirect('post.php?id=' . $post_id);
}
}
}
redirect('posts.php');