How to develop Projects(BlogApp) Using PHP and Mysql
Part 10
8️⃣ Blog Application
✅ Module 2: Comment System
🎯 Purpose:
Allow users to interact with blog posts by adding, viewing, and managing comments.
Functionalities:
- ✅ Add comment to a blog post
- ✅ View all comments on a blog post
- ✅ Admin/user can delete inappropriate comments
Project Structure:
│── /comments/
│ ├── add_comment.php # Submit a new comment
│ ├── comment_list.php # Show all comments for a blog post
│ ├── delete_comment.php # Delete comment (by admin or owner)
Database Table:
CREATE TABLE comments (
id INT AUTO_INCREMENT PRIMARY KEY,
blog_id INT NOT NULL,
user_id INT NOT NULL,
comment TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (blog_id) REFERENCES blogs(id) ON DELETE CASCADE,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);
➕
Add Comment Form (add_comment.php)
<form method="POST" action="add_comment.php">
<input type="hidden"
name="blog_id" value="1"> <!-- Blog ID -->
<input type="hidden"
name="user_id" value="2"> <!-- Logged in user ID
-->
<textarea
name="comment" placeholder="Write your comment..." required></textarea><br>
<button type="submit">Submit
Comment</button>
</form>
❌
Delete Comment Form (delete_comment.php)
<form method="POST" action="delete_comment.php">
<input type="number"
name="comment_id" placeholder="Comment ID to delete" required>
<button type="submit">Delete</button>
</form>