Skip to main content

Build Discussion Forum with PHP and MySQL

In this PHP project tutorial, we will explain how to develop your own discussion forum system with PHP and MySQL. We have created a running forum example in this tutorial and can download complete example code to enhance this to create own forum.

Forum systems is place for people where they can start discussions on topics. The forum is controlled by administrator or moderators who creates forum categories and manage permissions for forum.

The registered members can create own discussion topics or can participate in existing discussions. The guest members also allowed to create and participate in discussions.

Also, read:

So let’s start developing discussion forum system with PHP and MySQL. The major files are:

  • category.php: File to list categories and category topics.
  • Category.php: A class to keep methods related to category.
  • Topics.php: A class contains methods related to topics.
  • posts.php: File to list topic posts with quick reply editor.
  • Post.php: A class contains methods related to posts.

Step1: Create MySQL Database Table

As there are categories, topics, posts in forum, so first we will create MySQL database table forum_category to store categories.

CREATE TABLE `forum_category` (
  `category_id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `description` text NOT NULL,
  `is_category` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `forum_category`
  ADD PRIMARY KEY (`category_id`);
  
ALTER TABLE `forum_category`
  MODIFY `category_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=6;

We will create table forum_topics to store category topics.

CREATE TABLE `forum_topics` (
  `topic_id` int(11) NOT NULL,
  `subject` varchar(255) NOT NULL,
  `category_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `created` datetime NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `forum_topics`
  ADD PRIMARY KEY (`topic_id`);
  
ALTER TABLE `forum_topics`
  MODIFY `topic_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;

We will create table forum_posts to store topics posts.

CREATE TABLE `forum_posts` (
  `post_id` int(11) NOT NULL,
  `message` text NOT NULL,
  `topic_id` int(11) NOT NULL,
  `user_id` int(11) NOT NULL,
  `name` varchar(255) NOT NULL,
  `created` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `forum_posts`
  ADD PRIMARY KEY (`post_id`);
  
ALTER TABLE `forum_posts`
  MODIFY `post_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;

We will create table forum_users to store user.

CREATE TABLE `forum_users` (
  `user_id` int(11) NOT NULL,
  `username` varchar(255) NOT NULL,
  `password` varchar(50) NOT NULL,
  `name` varchar(255) NOT NULL,
  `email` varchar(50) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

ALTER TABLE `forum_users`
  ADD PRIMARY KEY (`user_id`);
  
ALTER TABLE `forum_users`
  MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;  

Step2: Category Listing

We will implement design and functionality to list forum categories. For this, we will make changes into category.php file. We will call method getCategoryList() from class Category.php to get category list.

<div class="single category">			
	<ul class="list-unstyled">
		<li><span style="font-size:25px;font-weight:bold;">Categories</span> <span class="pull-right"><span style="font-size:20px;font-weight:bold;">Topics / Posts</span></span></li>
		<?php
		$result = $categories->getCategoryList();
		while ($category = $result->fetch_assoc()) {
			$categories->category_id = $category['category_id'];
			$totalTopic = $categories->getCategoryTopicsCount();
			$totalPosts = $categories->getCategoryPostsCount();
		?>
		<li><a href="category.php?category_id=<?php echo $category['category_id'];?>" title=""><?php echo $category['name']; ?> <span class="pull-right"><?php echo $totalTopic; ?> / <?php echo $totalPosts; ?></span></a></li>			
		<?php } ?>
	</ul>
</div>

We will implement method getCategoryList() in class Category.php to get categories from MySQL database table forum_category and return.

public function getCategoryList(){		
	$sqlQuery = "
		SELECT *
		FROM ".$this->categoryTable." ORDER BY category_id DESC";
	
	$stmt = $this->conn->prepare($sqlQuery);
	$stmt->execute();
	$result = $stmt->get_result();			
	return $result;	
}

Step3: Topics Listing

We will make design changes and implement functionality to list visited category topics. For this, we will make changes in category.php file and call method getTopicList() from class Topic.php to list category topics.

<div class="single category">
	<?php 
	$categories->category_id = $_GET['category_id'];
	$categoryDetails = $categories->getCategory();
	?>
	<span style="font-size:20px;"><a href="category.php"><< <?php echo $categoryDetails['name']; ?></a></span>
	<br>	<br>		
	<ul class="list-unstyled">
		<li><span style="font-size:20px;font-weight:bold;">Topics</span> <span class="pull-right"><span style="font-size:15px;font-weight:bold;">Posts</span></span></li>
		<?php
		$topics->category_id = $_GET['category_id'];
		$result = $topics->getTopicList();
		while ($topic = $result->fetch_assoc()) {
			$topics->topic_id = $topic['topic_id'];
			$totalTopicPosts = $topics->getTopicPostCount();
		?>
		<li><a href="post.php?topic_id=<?php echo $topic['topic_id'];?>" title=""><?php echo $topic['subject']; ?> <span class="pull-right"><?php echo $totalTopicPosts; ?></span></a></li>			
		<?php } ?>
	</ul>
</div>	

We will implement method getTopicList() from class Topic.php to get category topics list from table category_topics to list topics.

public function getTopicList(){	
	if($this->category_id) {
		$sqlQuery = "
			SELECT c.name, c.category_id, t.subject, t.topic_id, t.user_id, t.created 			
			FROM ".$this->topicTable." as t 
			LEFT JOIN ".$this->categoryTable." as c ON t.category_id = c.category_id
			WHERE t.category_id = ".$this->category_id."
			ORDER BY t.topic_id DESC";			
		$stmt = $this->conn->prepare($sqlQuery);
		$stmt->execute();
		$result = $stmt->get_result();			
		return $result;	
	}
}

Step4: Posts Listing

We will implement functionality in file post.php to list post replies of topic. For this, we will call method getPosts() from class Topic.php to list topic posts.

<div class="posts list">
	<?php 
	$topics->topic_id = $_GET['topic_id'];
	$topicDetails = $topics->getTopic();
	?>
	<span style="font-size:20px;"><a href="category.php?category_id=<?php echo $topicDetails['category_id']; ?>"><< <?php echo $topicDetails['subject']; ?></a></span>
	<br><br>
	<?php				
	$result = $topics->getPosts();
	while ($post = $result->fetch_assoc()) {
		$date = date_create($post['created']);
		$posterName = $post['username'];
		if($posterName == '') {
			$posterName = $post['name'];
		}
	?>
	<article class="row" id="postRow_<?php echo $post['post_id']; ?>">
		<div class="col-md-2 col-sm-2 hidden-xs">
		  <figure class="thumbnail">
			<img class="img-responsive" src="images/user-avatar.png" />
			<figcaption class="text-center"><?php echo ucwords($posterName); ?></figcaption>
		  </figure>
		</div>
		<div class="col-md-10 col-sm-10">
		  <div class="panel panel-default arrow left">
			<div class="panel-body">
			  <header class="text-left">
				<div class="comment-user"><i class="fa fa-user"></i> By: <?php echo $posterName; ?></div>
				<time class="comment-date" datetime="16-12-2014 01:05"><i class="fa fa-clock-o"></i> <?php echo date_format($date, 'd M Y H:i:s'); ?></time>
			  </header>
			  <br>					  
			  <div class="comment-post"  id="post_message_<?php echo $post['post_id']; ?>">
				
				<?php echo $post['message']; ?>
				
			  </div>
			  
			  <textarea name="message" data-topic-id="<?php echo $post['topic_id']; ?>" id="<?php echo $post['post_id']; ?>" style="visibility: hidden;"></textarea><br>
			  
			  <div class="text-right" id="button_section_<?php echo $post['post_id']; ?>">
				<a class="btn btn-default btn-sm" id="edit_<?php echo $post['post_id']; ?>"><i class="fa fa-reply"></i> Edit</a>
				<a class="btn btn-default btn-sm"><i class="fa fa-reply"></i> Delete</a>
			  </div>			
				
			<div id="editSection_<?php echo $post['post_id']; ?>" class="hidden">						
				<button type="submit" id="save_<?php echo $post['post_id']; ?>" name="save" class="btn btn-info saveButton">Save</button>
				<button type="submit" id="cancel_<?php echo $post['post_id']; ?>" name="cancel" class="btn btn-info saveButton">Cancel</button>
			</div>					
			</div>					
	
		  </div>
		</div>
	</article>	
	<?php } ?>				
</div>

We will implement method getPosts() in class Topic.php to get topic posts from table category_posts to list them.

public function getPosts(){	
	if($this->topic_id) {
		$sqlQuery = "
			SELECT t.topic_id, p.post_id, p.message, p.topic_id, p.user_id, p.created, u.username			
			FROM ".$this->topicTable." as t 
			LEFT JOIN ".$this->postTable." as p ON t.topic_id = p.post_id
			LEFT JOIN ".$this->userTable." as u ON p.user_id = u.user_id
			WHERE p.topic_id = ".$this->topic_id."
			ORDER BY p.post_id ASC";			
		$stmt = $this->conn->prepare($sqlQuery);
		$stmt->execute();
		$result = $stmt->get_result();			
		return $result;	
	}
}

You may also like:

You can view the live demo from the Demo link and can download the script from the Download link below.
Demo Download

2 thoughts on “Build Discussion Forum with PHP and MySQL

  1. I added some posts and Categories in DB but only got one post
    so i changed Topic.php on line 49 from
    “LEFT JOIN “.$this->postTable.” as p ON t.topic_id = p.post_id”
    to
    “LEFT JOIN “.$this->postTable.” as p ON t.topic_id = p.topic_id”

  2. Please help me, this tutorial is fun, the only thing that I don’t understand is how to let user login and after they can post .

Comments are closed.