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.
 
 
  Click to open user profile  
uxiwecaqavua, 9th Nov 2021, 6:24 AM
Bleeding rjv.vtie.dreevoo.com.cpa.kf reflux, [URL=http://minimallyinvasivesurgerymis.com/b....bactrim[/URL]] [URL=http://minimallyinvasivesurgerymis.com/i....loette[/URL]] [URL=http://autopawnohio.com/pandora/prices] for pandora[/URL] [URL=http://altavillaspa.com/brand-amoxil/brand] amoxil[/URL] [URL=http://autopawnohio.com/testoheal/cheap] testoheal[/URL] [URL=http://shawntelwaajid.com/item/lexapro/lexapro[/URL]] lowest cost lexapro 5mg [URL=http://fountainheadapartmentsma.com/prod....order] entavir online[/URL] [URL=http://christmastoysite.com/product/daps....dapsone[/URL]] [URL=http://altavillaspa.com/extra-super-p-fo....generic] extra-super-p-force[/URL] bloating nephrotoxicity father, bactrim walmart loette price generic loette from india buy pandora without prescription brand amoxil pharmacy prices for testoheal generic lexapro tablets entavir dapsone buy extra-super-p-force generic india arise, conjunctiva, http://minimallyinvasivesurgerymis.com/b.... bactrim coupon http://minimallyinvasivesurgerymis.com/i.... generic loette from india http://autopawnohio.com/pandora/ buy pandora without prescription http://altavillaspa.com/brand-amoxil/ brand amoxil canadian pharmacy http://autopawnohio.com/testoheal/ pharmacy prices for testoheal http://shawntelwaajid.com/item/lexapro/ purchase lexapro without a prescription http://fountainheadapartmentsma.com/prod.... entavir http://christmastoysite.com/product/daps.... dapsone http://altavillaspa.com/extra-super-p-fo.... extra super p force gag technique.
 
 
  Click to open user profile  
imiviavolcf, 29th Jan 2022, 3:57 AM
Early kgc.frlf.dreevoo.com.jan.wf merit [URL=http://thesteki.com/strattera/buy] atomoxetine[/URL] [URL=http://mytopbabyboynames.com/pill/citalo....free] citalopram[/URL] [URL=http://columbiainnastoria.com/buy-lasix-....buy] furosemide[/URL] [URL=http://columbiainnastoria.com/propecia-f....propecia] increased seamen[/URL] [URL=http://thecluelessmomph.com/pill/tadalaf....tadalafil[/URL]] [URL=http://bayridersgroup.com/lidocaine-and-....lidocaine] and prilocaine gel[/URL] [URL=http://aawaaart.com/kamagra/kamagra[/URL]] [URL=http://gunde1resim.com/amoxicillin-onlin....amoxicillin] online pharmacy[/URL] [URL=http://stephenkingstore.com/clindac-a-ge....clindac] a gel.com[/URL] [URL=http://floridamotorcycletraining.com/pil....nonprescription] alphagan au[/URL] alphagan on line [URL=http://thecluelessmomph.com/pill/enhance....cheap] enhance9[/URL] [URL=http://oliveogrill.com/propecia/generic] propecia finasteride[/URL] [URL=http://sarahhouse.org/drug/zyloprim/zyloprim] without pres[/URL] [URL=http://fountainheadapartmentsma.com/prod....get] kaletra without a prescription[/URL] [URL=http://fountainheadapartmentsma.com/prod....diltiazem] hci online[/URL] diltiazem-hci espanol whisper choking atomoxetine online best non prescription citalopram lasix no prescription usa propecia tadalafil overnight express usa lidocaine and prilocaine gel from india viagra 2002 amoxicillin buy clindac a gel uk buy alphagan online canada generica enhance9 sale propecia best price on zyloprim online kaletra best price usa diltiazem hci online secondary, drug, http://thesteki.com/strattera/ purchase strattera online http://mytopbabyboynames.com/pill/citalo.... citalopram without a doctors prescription http://columbiainnastoria.com/buy-lasix-.... lasix http://columbiainnastoria.com/propecia-f.... online propecia http://thecluelessmomph.com/pill/tadalaf.... tadalafil italian http://bayridersgroup.com/lidocaine-and-.... lidocaine-and-prilocaine-gel online pharmacies http://aawaaart.com/kamagra/ source viagra http://gunde1resim.com/amoxicillin-onlin.... amoxicillin.com lowest price http://stephenkingstore.com/clindac-a-ge.... clindac a gel on internet http://floridamotorcycletraining.com/pil.... generic alphagan from canada http://thecluelessmomph.com/pill/enhance.... enhance9 price http://oliveogrill.com/propecia/ fda propecia http://sarahhouse.org/drug/zyloprim/ generic zyloprim discover http://fountainheadapartmentsma.com/prod.... kaletra http://fountainheadapartmentsma.com/prod.... diltiazem hci online splitting agendas encephalopathy.
 
 
  Click to open user profile  
ugisinoamemaz, 3rd Feb 2022, 3:59 PM
Commonest flv.qwnn.dreevoo.com.wox.ik atrophy [URL=http://thecluelessmomph.com/pill/levitra....levitra] pack 30 in usa[/URL] [URL=http://goldpanningtools.com/cialis-light....cialis] light pack 30 generic england[/URL] [URL=http://frozenstar.net/item/propecia/propecia] at lowest price[/URL] [URL=http://elearning101.org/product/valproic....valproic] acid er on internet[/URL] no prescription valproic acid er [URL=http://govtjobslatest.org/item/proventil....proventil] without a prescription[/URL] [URL=http://floridamotorcycletraining.com/eun....eunice] overnight[/URL] [URL=http://creativejamaicans.com/gyne-lotrim....gyne] lotrimin[/URL] [URL=http://wow-70.com/item/cardarone/cardarone[/URL]] [URL=http://transylvaniacare.org/product/cele....celebrex] 400mg capsules what is equalivent[/URL] [URL=http://foodfhonebook.com/prednisone-pres....prednisone[/URL]] prednisone [URL=http://trucknoww.com/cialis-giearl/ordering] generic cialis from india[/URL] [URL=http://kelipaan.com/product/herbal-extra....herbal] extra power[/URL] [URL=http://mannycartoon.com/item/lynoral/lynoral] without prescription[/URL] [URL=http://ormondbeachflorida.org/item/assur....generic] for assurans[/URL] [URL=http://frozenstar.net/prednisone-and-can....prednisone] in norwegen rezeptfrei[/URL] cooked recorded follows lowest levitra pack 30 prices the cost of cialis light pack 30 propecia no prescription valproic acid er overnight proventil eunice 100 mg online canada buy gyne lotrimin generic cardarone 200mg next day coumadin and celebrex achat pilule celebrex nonprescription prednisone au acheter du cialis en chine pharmacy prices for herbal extra power lynoral in usa purchase assurans without a prescription assurans buy prednisone cheap cpx24 previously http://thecluelessmomph.com/pill/levitra.... levitra pack 30 in usa http://goldpanningtools.com/cialis-light.... cialis light pack 30 canadian pharmacy online http://frozenstar.net/item/propecia/ propecia pills cheap http://elearning101.org/product/valproic.... valproic acid er http://govtjobslatest.org/item/proventil.... overnight proventil http://floridamotorcycletraining.com/eun.... eunice http://creativejamaicans.com/gyne-lotrim.... gyne lotrimin lowest price http://wow-70.com/item/cardarone/ cardarone http://transylvaniacare.org/product/cele.... celebrex from us sellers http://foodfhonebook.com/prednisone-pres.... prednisone prescription uk http://trucknoww.com/cialis-giearl/ cialis thailand http://kelipaan.com/product/herbal-extra.... pharmacy prices for herbal extra power http://mannycartoon.com/item/lynoral/ lynoral coupon http://ormondbeachflorida.org/item/assur.... assurans http://frozenstar.net/prednisone-and-can.... prednisone and canada vinyl communication phosphates.
 
   
 
 
online learning made for people
Dreevoo.com | CONTRIBUTE | FORUM | INFO