3
This commit is contained in:
parent
96ee530283
commit
8989de90b0
25
db/migrations/002_create_comments_table.php
Normal file
25
db/migrations/002_create_comments_table.php
Normal file
@ -0,0 +1,25 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/../config.php';
|
||||
|
||||
function migrate_002_create_comments_table() {
|
||||
try {
|
||||
$pdo = db();
|
||||
$sql = "
|
||||
CREATE TABLE IF NOT EXISTS `comments` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`post_id` int(11) NOT NULL,
|
||||
`name` varchar(255) NOT NULL,
|
||||
`comment` text NOT NULL,
|
||||
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `post_id` (`post_id`),
|
||||
CONSTRAINT `comments_ibfk_1` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
";
|
||||
$pdo->exec($sql);
|
||||
echo "Migration 002: Comments table created successfully.\n";
|
||||
} catch (PDOException $e) {
|
||||
die("Migration 002 failed: " . $e->getMessage() . "\n");
|
||||
}
|
||||
}
|
||||
|
||||
58
post.php
58
post.php
@ -8,12 +8,39 @@ if (!$post_id || !filter_var($post_id, FILTER_VALIDATE_INT)) {
|
||||
exit;
|
||||
}
|
||||
|
||||
// Handle comment submission
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'create_comment') {
|
||||
$name = trim($_POST['name'] ?? '');
|
||||
$comment_text = trim($_POST['comment'] ?? '');
|
||||
|
||||
if (!empty($name) && !empty($comment_text)) {
|
||||
try {
|
||||
$pdo = db();
|
||||
$stmt = $pdo->prepare("INSERT INTO comments (post_id, name, comment) VALUES (?, ?, ?)");
|
||||
$stmt->execute([$post_id, $name, $comment_text]);
|
||||
header("Location: post.php?id=" . $post_id);
|
||||
exit;
|
||||
} catch (PDOException $e) {
|
||||
error_log("DB Error: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$post = null;
|
||||
$comments = [];
|
||||
try {
|
||||
$pdo = db();
|
||||
|
||||
// Fetch post
|
||||
$stmt = $pdo->prepare("SELECT * FROM posts WHERE id = ?");
|
||||
$stmt->execute([$post_id]);
|
||||
$post = $stmt->fetch();
|
||||
|
||||
// Fetch comments
|
||||
$stmt = $pdo->prepare("SELECT * FROM comments WHERE post_id = ? ORDER BY created_at DESC");
|
||||
$stmt->execute([$post_id]);
|
||||
$comments = $stmt->fetchAll();
|
||||
|
||||
} catch (PDOException $e) {
|
||||
error_log("DB Error: " . $e->getMessage());
|
||||
}
|
||||
@ -68,6 +95,37 @@ if (!$post) {
|
||||
<?php echo nl2br(htmlspecialchars($post['content'])); ?>
|
||||
</section>
|
||||
</article>
|
||||
|
||||
<section class="mb-5">
|
||||
<div class="card bg-light">
|
||||
<div class="card-body">
|
||||
<!-- Comment form-->
|
||||
<form class="mb-4" method="POST">
|
||||
<input type="hidden" name="action" value="create_comment">
|
||||
<div class="mb-3">
|
||||
<label for="name" class="form-label">Your Name</label>
|
||||
<input type="text" class="form-control" id="name" name="name" required>
|
||||
</div>
|
||||
<div class="mb-3">
|
||||
<label for="comment" class="form-label">Your Comment</label>
|
||||
<textarea class="form-control" rows="3" id="comment" name="comment" required></textarea>
|
||||
</div>
|
||||
<button type="submit" class="btn btn-primary">Submit Comment</button>
|
||||
</form>
|
||||
<!-- Comments section-->
|
||||
<?php foreach ($comments as $comment): ?>
|
||||
<div class="d-flex mb-4">
|
||||
<div class="flex-shrink-0"><img class="rounded-circle" src="https://dummyimage.com/50x50/ced4da/6c757d.jpg" alt="..." /></div>
|
||||
<div class="ms-3">
|
||||
<div class="fw-bold"><?php echo htmlspecialchars($comment['name']); ?></div>
|
||||
<?php echo htmlspecialchars($comment['comment']); ?>
|
||||
</div>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<a href="/" class="btn btn-outline-primary">← Back to all posts</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user