Create a Simple Forum Using PHP/MYSQL

0
230

Good day everyone! Today, I will teach you how to create a SIMPLE FORUM Using PHP/MYSQL. This tutorial gives a big help for all web developers who wants to develop their own forum site.

Like the real forum, in this tutorial you can create your forum with add topic board function, post threads, comment to threads, and count all the topics posted in each topic board.

So first, create a database, name it as any name you desire. In my case, I use “itsourcecode” as the name of the database.

Then create a table name, it as “boards” then put the following attributes.

  1. CREATE TABLE `boards` ( `board_id` int(11) NOT NULL, `board_name` text NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

Then create again another table, name it as “threads” then put the following attributes.

  1. CREATE TABLE `threads` ( `thread_id` int(11) NOT NULL, `board_id` int(11) NOT NULL, `thread_title` text NOT NULL, `thread_content` text NOT NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1;

For the connection to the database, create “connection.php” file then put the following codes.

  1. <?php
  2. $mysqli = new mysqli('localhost', 'root', '', 'itsourcecode');
  3. ?>

In the ‘’index.php” file, put the following codes.

  1. <?php include 'connection.php' ?>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>ITSOURCECODE Simple Forum</title>
  7. </head>
  8. <body>
  9. <h3>ITSOURCECODE Simple Forum in PHP/MYSQL</h3>
  10. <a href="add-board.php">Add New Topic Board</a><br>
  11. <?php
  12. $boards = $mysqli->query("SELECT * FROM boards");
  13. while ($board_data = $boards->fetch_assoc()) {
  14. $threads = $mysqli->query("SELECT * FROM threads WHERE board_id = ".$board_data['board_id']."");
  15. ?>
  16. <br>
  17. <b>#<?php echo $board_data['board_id'] ?></b> <a href="view-board.php?board_id=<?php echo $board_data['board_id'] ?>"><?php echo $board_data['board_name'] ?></a> [<b><?php echo $threads->num_rows ?></b> Topics]
  18. <?php }
  19. if ($boards->num_rows == null) {
  20. echo '<br>no topic board created yet';
  21. }
  22. ?>
  23. </body>
  24. </html>

For adding new topic board, create “add-board.php” file then put the following codes.

  1. <?php include 'connection.php'; ?>
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>Add New Topic Board</title>
  7. </head>
  8. <body>
  9. <a href="index.php">Home</a> | Add New Topic Board<br><br>
  10. <form method="post" action="add-board-action.php">
  11. <label>Board title:</label><br>
  12. <input type="text" name="title" /><br><br>
  13. <input type="submit" name="add_title" value="Add" />
  14. </form>
  15. </body>
  16. </html>

For the add board action, create “add-board-action.php” file then put the following codes.

  1. <?php
  2. include 'connection.php';
  3. if (isset($_POST['add_title'])) {
  4. $title = $_POST['title'];
  5.  
  6. $add_title = $mysqli->query("INSERT INTO boards (board_name) VALUES ('$title')");
  7. if ($add_title) {
  8. header("Location: index.php");
  9. } else {
  10. echo $mysqli->error;
  11. }
  12. }
  13. ?>

And now, for viewing the created topic board, create a “view-board.php” file then put the following codes.

  1. <?php
  2. include 'connection.php';
  3. $board_id = $_GET['board_id'];
  4. $get_board = $mysqli->query("SELECT * FROM boards WHERE board_id = $board_id");
  5. $board_data = $get_board->fetch_assoc();
  6. ?>
  7. <!DOCTYPE html>
  8. <html>
  9. <head>
  10. <meta charset="UTF-8">
  11. <title><?php echo $board_data['board_name'] ?></title>
  12. </head>
  13. <body>
  14. <a href="index.php">Home</a> | <b><?php echo $board_data['board_name']; ?></b><br><br>
  15. <a href="add-thread.php?board_id=<?php echo $board_id ?>">Post New Thread</a><br><br>
  16. <?php
  17. $threads = $mysqli->query("SELECT * FROM threads WHERE board_id = $board_id");
  18. while ($thread_data = $threads->fetch_assoc()) { ?>
  19. <b>#<?php echo $thread_data['thread_id'] ?></b> <a href="view-thread.php?thread_id=<?php echo $thread_data['thread_id'] ?>&board_id=<?php echo $board_id ?>"><?php echo $thread_data['thread_title'] ?></a>
  20. <?php }
  21. if ($threads->num_rows == null) {
  22. echo '<br><br>no threads posted yet';
  23. }
  24. ?>
  25. </body>
  26. </html>

For posting threads to every board. Create a “add-thread.php” file then put the following codes.

  1. <?php
  2. include 'connection.php';
  3. $board_id = $_GET['board_id'];
  4. $get_board = $mysqli->query("SELECT * FROM boards WHERE board_id = $board_id");
  5. $board_data = $get_board->fetch_assoc();
  6. ?>
  7. <!DOCTYPE html>
  8. <html>
  9. <head>
  10. <meta charset="UTF-8">
  11. <title>Add New Thread</title>
  12. </head>
  13. <body>
  14. <a href="index.php">Home</a> | <?php echo $board_data['board_name'] ?> | <b>Add Thread</b><br><br>
  15. <form method="post" action="add-thread-action.php?board_id=<?php echo $board_id ?>">
  16. <label>Title:</label><br>
  17. <input type="text" name="title" /><br>
  18. <label>Content:</label><br>
  19. < t e x t a r e a name="content" >< / t e x t a r e a ><br><br>
  20. <input type="submit" name="post_thread" value="Post" />
  21. </form>
  22. </body>
  23. </html>

Then for the action in adding thread. Create a “add-thread-action.php” file then put the following codes.

  1. <?php
  2. include 'connection.php';
  3. if (isset($_POST['post_thread'])) {
  4. $board_id = $_GET['board_id'];
  5. $title = $_POST['title'];
  6. $content = $_POST['content'];
  7.  
  8. $post_thread = $mysqli->query("INSERT INTO threads (board_id, thread_title, thread_content) VALUES ($board_id, '$title', '$content')");
  9. if ($post_thread) {
  10. header("Location: view-board.php?board_id=$board_id");
  11. } else {
  12. echo $mysqli->error;
  13. }
  14. }
  15. ?>

For viewing the posted topic. Create a “view-thread.php” file then put the following codes.

  1. <?php
  2. include 'connection.php';
  3. $board_id = $_GET['board_id'];
  4. $thread_id = $_GET['thread_id'];
  5.  
  6. $get_board = $mysqli->query("SELECT * FROM boards WHERE board_id = $board_id");
  7. $board_data = $get_board->fetch_assoc();
  8.  
  9. $get_thread = $mysqli->query("SELECT * FROM threads WHERE thread_id = $thread_id");
  10. $thread_data = $get_thread->fetch_assoc();
  11. ?>
  12. <!DOCTYPE html>
  13. <html>
  14. <head>
  15. <meta charset="UTF-8">
  16. <title><?php echo $thread_data['thread_title'] ?></title>
  17. </head>
  18. <body>
  19. <a href="index.php">Home</a> | <a href="view-board.php?board_id=<?php echo $board_id ?>"><?php echo $board_data['board_name'] ?></a> | <b><?php echo $thread_data['thread_title'] ?></b><br><br>
  20. Title: <b><?php echo $thread_data['thread_title'] ?></b><br><br>
  21. <b>Content:</b><br>
  22. <?php echo $thread_data['thread_content'] ?>
  23. </body>
  24. </html>

Forum Using PHP/MYSQL Screenshots:

If you have questions about ” Simple Forum Using PHP/MYSQL” feel free to ask us by commenting below or just visit on our contact page. Thank You.

Related Article you may like:

Facebook Comments
(Visited 239 times, 1 visits today)

LEAVE A REPLY

Please enter your comment!
Please enter your name here