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  
Tomaseluch, 22nd May 2021, 1:41 AM
Плитка с 14 стран, в том числе и [url=https://www.realgres.ru/manufacturer/cas....плитка] casalgrande padana[/url]
 
   
 
 
online learning made for people
Dreevoo.com | CONTRIBUTE | FORUM | INFO