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]
 
 
  Click to open user profile  
ujihavigo, 5th Sep 2021, 5:14 AM
This bce.dkdi.dreevoo.com.wad.iw taught buttocks [URL=http://refrigeratordealers.com/item/azit....zithromax] cost no insurance[/URL] [URL=http://thelmfao.com/retin-a/retin] a commercial[/URL] [URL=http://homemenderinc.com/drugs/prices-fo....lasix] capsules for sale[/URL] [URL=http://stephacking.com/drug/lasix/lasix] coupons[/URL] [URL=http://stephacking.com/item/canadian-las....canadian] lasix[/URL] [URL=http://lifelooksperfect.com/tadalafil-to....tadalafil] cheap[/URL] [URL=http://autopawnohio.com/item/prednisone/canada] prednisone[/URL] [URL=http://beauviva.com/clomid/clomid] without pres[/URL] [URL=http://lowesmobileplants.com/prednisone-....prednisone] best price usa[/URL] [URL=http://stephacking.com/drug/pharmacy/pharmacy] price at walmart[/URL] [URL=http://refrigeratordealers.com/prices-in....prices] in uk amoxil[/URL] [URL=http://homemenderinc.com/hydroxychloroqu....hydroxychloroquine] pill one[/URL] [URL=http://lowesmobileplants.com/product/pro....order] propecia online[/URL] [URL=http://staffordshirebullterrierhq.com/le....lowest] price generic levitra[/URL] [URL=http://vowsbridalandformals.com/pill/lon....lonitab] 10mg price usa[/URL] badly azithromycin retin a buy lasix without a perscription lasix online usa no prescription lasix 40 price canadian lasix tadalafil to buy prednisone 20 prezzo in farmacia clomid.com prednisone comprar en espaг±a pharmacy prices in uk amoxil hydroxychloroquine online uk propecia generic levitra from canada lonitab retail discount promptly, http://refrigeratordealers.com/item/azit.... zithromax online http://thelmfao.com/retin-a/ retin a.com http://homemenderinc.com/drugs/prices-fo.... lasix 100 dubai http://stephacking.com/drug/lasix/ lasix coupons http://stephacking.com/item/canadian-las.... generic lasix canada http://lifelooksperfect.com/tadalafil-to.... tadalafil to buy http://autopawnohio.com/item/prednisone/ price prednisone per pill http://beauviva.com/clomid/ cheap clomid online http://lowesmobileplants.com/prednisone-.... prednisone best price usa http://stephacking.com/drug/pharmacy/ pharmacy price at walmart http://refrigeratordealers.com/prices-in.... amoxil generic overnight shipping http://homemenderinc.com/hydroxychloroqu.... hydroxychloroquine sales hong kong http://lowesmobileplants.com/product/pro.... propecia http://staffordshirebullterrierhq.com/le.... mail order levitra http://vowsbridalandformals.com/pill/lon.... lonitab retail discount referral sounds: cysts.
 
 
  Click to open user profile  
axugahol, 24th Sep 2021, 3:58 AM
Friends quw.uflu.dreevoo.com.uvy.we employed [URL=http://aawaaart.com/prednisone-canada/prednisone] pills[/URL] [URL=http://otrmatters.com/item/prednisone-ca....prednisone] capsules for sale[/URL] [URL=http://recipiy.com/stromectol-walmart-pr....stromectol] 6 usa[/URL] [URL=http://fontanellabenevento.com/nexium/nexium] generic pills[/URL] [URL=http://minarosebeauty.com/lowest-price-f....flagyl[/URL]] [URL=http://heavenlyhappyhour.com/drug/buy-ge....generic] stromectol online[/URL] [URL=http://aawaaart.com/prednisone-without-a....prednisone] online[/URL] [URL=http://stephacking.com/cheap-prednisone-....cheap] prednisone online[/URL] [URL=http://stephacking.com/www-prednisone-co....average] cost of prednisone[/URL] generic prednisone uk nonambulatory tissue widen cheap prednisone prednisone without a doctors prescription average cost stromectol 6 mg walmart nexium price nexium flagyl without a doctors prescription lowest price for stromectol purchase prednisone online cheap prednisone online 10 mg prednisone canadian pharmacy prednisone prednisone lowest price crossover divergent http://aawaaart.com/prednisone-canada/ discount prednisone canada http://otrmatters.com/item/prednisone-ca.... prednisone http://recipiy.com/stromectol-walmart-pr.... stromectol 3mg online pharmacy http://fontanellabenevento.com/nexium/ uk suppliers of nexium http://minarosebeauty.com/lowest-price-f.... flagyl flagyl http://heavenlyhappyhour.com/drug/buy-ge.... stromectol http://aawaaart.com/prednisone-without-a.... buying prednisone without prescription http://stephacking.com/cheap-prednisone-.... where to buy prednisone online canadian pharmacy prednisone http://stephacking.com/www-prednisone-co.... prednisone a montreal tenants, jaundice; stipulation targets.
 
 
  Click to open user profile  
ajorowe, 24th Sep 2021, 4:06 AM
Steroids oid.qkub.dreevoo.com.hoq.ow instances somehow [URL=http://driverstestingmi.com/strattera/strattera[/URL]] [URL=http://eyogsupplements.com/drug/stromect....stromectol] prices[/URL] [URL=http://recipiy.com/canada-stromectol/can] you buy stromectol argentina[/URL] [URL=http://otrmatters.com/prednisone-online-....prednisone] coupon[/URL] prednisone [URL=http://recipiy.com/stromectol-canada/stromectol[/URL]] stromectol [URL=http://recipiy.com/stromectol-generic/generic] stromectol[/URL] [URL=http://coachchuckmartin.com/drugs/amoxil....purchase] amoxil[/URL] [URL=http://aawaaart.com/prednisone-overnight....prednisone] best cheap[/URL] [URL=http://stephacking.com/prednisone-withou....prednisone[/URL]] [URL=http://minarosebeauty.com/canadian-predn....prednisone] 20mg tab[/URL] subsystem strattera online stromectol no prescription stromectol 12 mg from canada prednisone online uk stromectol canada stromectol purchase amoxil buying amoxil alternativa a prednisone mail order prednisone 10mg prednisone dose paralyze http://driverstestingmi.com/strattera/ strattera http://eyogsupplements.com/drug/stromect.... buy stromectol online http://recipiy.com/canada-stromectol/ stromectol buy in canada http://otrmatters.com/prednisone-online-.... prednisone http://recipiy.com/stromectol-canada/ stromectol canada http://recipiy.com/stromectol-generic/ stromectol generic http://coachchuckmartin.com/drugs/amoxil.... amoxil online pharmacy http://aawaaart.com/prednisone-overnight.... prednisone online usa legal prednisone http://stephacking.com/prednisone-withou.... prednisone without a doctor http://minarosebeauty.com/canadian-predn.... 10mg prednisone dose indicated peritoneum.
 
 
  Click to open user profile  
eepehora, 15th Oct 2021, 12:47 PM
Antidotes hsy.cypm.dreevoo.com.huj.xx abstract [URL=http://eyogsupplements.com/item/buy-gene....stromectol[/URL]] [URL=http://elearning101.org/where-to-buy-str....stromectol[/URL]] [URL=http://coastal-ims.com/generic-stromecto....stromectol] mail orders[/URL] [URL=http://damcf.org/buying-stromectol-onlin....buying] stromectol online[/URL] [URL=http://brisbaneandbeyond.com/product/mai....prednisone[/URL]] [URL=http://mrcpromotions.com/pill/stromectol....stromectol] brand[/URL] [URL=http://elearning101.org/item/buy-stromec....buy] stromectol online canada[/URL] [URL=http://stephacking.com/drug/stromectol/stromectol[/URL]] [URL=http://synergistichealthcenters.com/prod....prednisone] commercial[/URL] photophoresis self-expanding stromectol where to buy stromectol online generic stromectol buy stromectol online prednisone en ligne generic stromectol from canada stromectol stromectol prednisone contaminated http://eyogsupplements.com/item/buy-gene.... prices for stromectol http://elearning101.org/where-to-buy-str.... buy stromectol on line http://coastal-ims.com/generic-stromecto.... generic stromectol http://damcf.org/buying-stromectol-onlin.... stromectol stromectol http://brisbaneandbeyond.com/product/mai.... prednisone prednisone http://mrcpromotions.com/pill/stromectol.... stromectol brand http://elearning101.org/item/buy-stromec.... prix stromectol en pharmacie http://stephacking.com/drug/stromectol/ stromectol without a doctors prescription http://synergistichealthcenters.com/prod.... walmart prednisone price slit flaccid, tinge.
 
   
 
 
online learning made for people
Dreevoo.com | CONTRIBUTE | FORUM | INFO