diff --git a/db/migrations/002_create_comments_table.php b/db/migrations/002_create_comments_table.php new file mode 100644 index 0000000..a561a99 --- /dev/null +++ b/db/migrations/002_create_comments_table.php @@ -0,0 +1,25 @@ +exec($sql); + echo "Migration 002: Comments table created successfully.\n"; + } catch (PDOException $e) { + die("Migration 002 failed: " . $e->getMessage() . "\n"); + } +} + diff --git a/post.php b/post.php index 52e24bc..b4a62f8 100644 --- a/post.php +++ b/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) { + +
+
+
+ +
+ +
+ + +
+
+ + +
+ +
+ + +
+
...
+
+
+ +
+
+ +
+
+
+ ← Back to all posts