Dreevoo.com | Online Learning and Knowledge Sharing
 
Home | Programs | Programming languages | PHP & MySQL | How to create simple forum in PHP from scratch
Guest
Click to view your profile
Topics
Programs
Languages
Recipes
Home
Shortcuts
 
 

How to create simple forum in PHP from scratch

In this lesson we will cover most of the basic PHP knowledge and will learn how to create a very simple forum by using PHP and MySql database.

 
  Author: podtalje | Version: PHP | 31st May 2013 |  
 
 
1.
 

First we will create MySql database for storing data with the following fields:

id - int
user - text
message - text
date - datetime


You can read more about creating database in tutorial:

 

 
 
2.
 

Now we will create a form for posting messages.

The following code should be put between <head> and </head> tags.

<form method="get" action="forum.php">
<p>User:
  <label for="user"></label>
  <input type="text" name="user" id="user" />
  <br />
</p>
<p>Message: <br />
  <label for="message"></label>
  <textarea name="message" id="message" cols="45" rows="5"></textarea>
</p>
<p>
  <input type="submit" name="submit" id="submit" value="Post message" />
</p>
</form>



To learn about forms and PHP, please see tutorial:


 
 
3.
 

You should get a result that is shown on the picture.

 
 
4.
 

At the start of the script we will first connect to the database.

Insert the code below at the top of your php script>

<?
$mysqli = new mysqli("localhost", "root", "", "my_db");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
?>


You should also set correct hostname, username, password and database name for your database.

You can learn more about using PHP and MySql in tutorial:



 
 
5.
 

After establishing connection to MySql database we will check if there was new message posted.

If this is the case we will insert the data into our database.

if (isset($_GET['message'])) {
   
    $user=$mysqli->real_escape_string($_GET['user']);
    $message=$mysqli->real_escape_string($_GET['message']);
    $date=date('Y-m-d H:i:s');
   
    $sql="INSERT INTO forum(id, user, message, date) VALUES(0,'$user','$message','$date')";
    $mysqli->query($sql);
}


 
 
6.
 

Now all that is left to do is to show all the messages from the database.

This code should be placed after the <body> tag.

<?
$sql = "SELECT * FROM forum";
$result = $mysqli->query($sql);

while($row = $result->fetch_assoc()) {
  echo $row['user'].',  '.$row['date'].' <br />';
  echo $row['message'].'<br />';
  echo '------------------------ <br />';
}
?>


To learn more about SQL, please see tutorial:


 
 
7.
 

Now your simple forum is ready and you can use it to post messages.

Congratulations.


Below is also a complete code so it will be easier for you to copy it to your php script:

<?
$mysqli = new mysqli("localhost", "root", "", "my_db");
if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
   
if (isset($_GET['message'])) {
   
    $user=$mysqli->real_escape_string($_GET['user']);
    $message=$mysqli->real_escape_string($_GET['message']);
    $date=date('Y-m-d H:i:s');
   
    $sql="INSERT INTO forum(id, user, message, date) VALUES(0,'$user','$message','$date')";
    $mysqli->query($sql);
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP and MySql</title>
</head>

<body>
<h2>My First Forum</h2>

<?
$sql = "SELECT * FROM forum";
$result = $mysqli->query($sql);

while($row = $result->fetch_assoc()) {
  echo $row['user'].',  '.$row['date'].' <br />';
  echo $row['message'].'<br />';
  echo '------------------------ <br />';
}
?>

<form method="get" action="forum.php">
<p>User:
  <label for="user"></label>
  <input type="text" name="user" id="user" />
  <br />
</p>
<p>Message: <br />
  <label for="message"></label>
  <textarea name="message" id="message" cols="45" rows="5"></textarea>
</p>
<p>
  <input type="submit" name="submit" id="submit" value="Post message" />
</p>
</form>

</body>
</html>

 
 
 
   
  Please login to post a comment
  Click to open user profile  
theicon, 20th Dec 2013, 3:31 AM
this is excellent..... simple and straight forward

 
 
  Click to open user profile  
julien gautar, 7th Apr 2014, 5:33 AM
Ohhh this is excellent...You just saved me... I am taking a PHP course and I have an assignment which requires exactly what this tutorial is about...I had no idea how to solve it. Thank you very much!
 
 
  Click to open user profile  
deepender, 15th Sep 2015, 11:46 AM
I really appreciate that you wrote this article and shared some really good information on this specific topic. I was in vital need to get some information on this topic and thanks to you I have got that! Thanks, once again!
visit for <a href="http://training.mstechnosoft.com">php training in Jaipur</a>
 
 
  Click to open user profile  
Skill4Rex2, 3rd Feb 2016, 3:37 PM

 
 
  Click to open user profile  
Skill4Rex2, 3rd Feb 2016, 3:37 PM

 
 
  Click to open user profile  
Skill4Rex2, 3rd Feb 2016, 4:07 PM

 
 
  Click to open user profile  
sarah0831, 12th Mar 2016, 9:38 AM
thank you for this tutorial. it really help me
 
 
  Click to open user profile  
RitaMadore125, 4th Jul 2016, 7:28 AM
May, on the off chance that somebody is sufficiently fit todo plates free weight seat press per side then megadrox he/she can perform plates seat press machineeffortlessly. The most ideal method for getting megadrox review appropriate shape is to stir up the freeweight preparing with machine work outs. Along these lines the http://healthnbeautyfacts.com/megadrox-r....
 
 
  Click to open user profile  
hemant, 31st Aug 2016, 3:33 PM
I am in search of such type of script to create my own forum.Thanks a lot for this useful post we will try to deliver it maximum needers as we are involve in <a href='http://www.dzone.co.in'>ASP .NET and PHP training in Jaipur</a>.Thanks once again for you tutorial.
 
   
 
 
online learning made for people
Dreevoo.com | CONTRIBUTE | FORUM | INFO