how to create simple php forum or Discussion Board

By | June 23, 2015

how to create simple php forum or Discussion Board in your Website.

Many Web Developer wants to add a Simple discussion Board into Client Website.So in that purpose i will tell how to create simple php forum or Discussion Board in any Website without Installing any heavy Script.

Here i will teach you step by step.It is very easy to implement on your own ends.i will Also provide you full php code and database related content of this project.

Recommended article to read Create Dynamic Website in PHP MySQL with admin Panel.

Steps of how to create simple php forum or Discussion Board




Step 1 – Create Database and tables:

Create Database “Forum” and then creates two tables named as “Forum_question” and “forum_answer”
(note: if you want to change the database name and table names then you need to change it in all pages where you use this database or table).
You can create table and database manually in xampp, wamp or other web server platform.you may like to read how to Change Apache Port 8080 – Apache not running in xampp.Here is the query to make tables.

Table of Forum_question:

CREATE TABLE `forum_question` (
`id` int(4) NOT NULL auto_increment,
`topic` varchar(255) NOT NULL default ”,
`detail` longtext NOT NULL,
`name` varchar(65) NOT NULL default ”,
`email` varchar(65) NOT NULL default ”,
`datetime` varchar(25) NOT NULL default ”,
`view` int(4) NOT NULL default ‘0’,
`reply` int(4) NOT NULL default ‘0’,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 ;

Table of Forum_answer:

CREATE TABLE `forum_answer` (
`question_id` int(4) NOT NULL default ‘0’,
`a_id` int(4) NOT NULL default ‘0’,
`a_name` varchar(65) NOT NULL default ”,
`a_email` varchar(65) NOT NULL default ”,
`a_answer` longtext NOT NULL,
`a_datetime` varchar(25) NOT NULL default ”,
KEY `a_id` (`a_id`)
) ENGINE=MyISAM;

Now you have a database along with its tables :) .That’s it.

Step 2 – Create a page for Discussion topics (Create_topic.php):

This page is used to Add or create new Topic in Discussion board or forum.it will look like this:

how to create simple php forum or Discussion Board [howpk.com]

how to create simple php forum or Discussion Board [howpk.com]

Here is the code for this page.

<table width=”400″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”1″ bgcolor=”#CCCCCC”>
<tr>
<form id=”form1″ name=”form1″ method=”post” action=”add_topic.php”>
<td>
<table width=”100%” border=”0″ cellpadding=”3″ cellspacing=”1″ bgcolor=”#FFFFFF”>
<tr>
<td colspan=”3″ bgcolor=”#E6E6E6″><strong>Create New Topic</strong> </td>
</tr>
<tr>
<td width=”14%”><strong>Topic</strong></td>
<td width=”2%”>:</td>
<td width=”84%”><input name=”topic” type=”text” id=”topic” size=”50″ /></td>
</tr>
<tr>
<td valign=”top”><strong>Detail</strong></td>
<td valign=”top”>:</td>
<td><textarea name=”detail” cols=”50″ rows=”3″ id=”detail”></textarea></td>
</tr>
<tr>
<td><strong>Name</strong></td>
<td>:</td>
<td><input name=”name” type=”text” id=”name” size=”50″ /></td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td>:</td>
<td><input name=”email” type=”text” id=”email” size=”50″ /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type=”submit” name=”Submit” value=”Submit” /> <input type=”reset” name=”Submit2″ value=”Reset” /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

This is the simple forum.you can make it more attractive. :) .

Step 3 – Create a page to add the created topic into database (add_topic.php):

Before we just create a form to take input in terms of topic.We must have to save that form data into database in order to manipulate and fetch it.Here is the code for “add_topic.php” file.

<?php

$host=”localhost”; // Host name
$username=””; // Mysql username
$password=””; // Mysql password
$db_name=”Forum”; // Database name(change it with your if you create database of your choice name)
$tbl_name=”forum_question”; // Table name (change it with your if you create tables of your choice name)

// Connect to server and select database.
mysql_connect(“$host”, “$username”, “$password”)or die(“cannot connect”);
mysql_select_db(“$db_name”)or die(“cannot select DB”);

// get data that sent from form
$topic=$_POST[‘topic’];
$detail=$_POST[‘detail’];
$name=$_POST[‘name’];
$email=$_POST[’email’];

$datetime=date(“d/m/y h:i:s”); //create date time

$sql=”INSERT INTO $tbl_name(topic, detail, name, email, datetime)VALUES(‘$topic’, ‘$detail’, ‘$name’, ‘$email’, ‘$datetime’)”;
$result=mysql_query($sql);

if($result){
echo “Successful<BR>”;
echo “<a href=main_forum.php>View your topic</a>”;
}
else {
echo “ERROR”;
}
mysql_close();
?>

Step 4 – Create a page which show all Topic means main page of Discussion board (main_forum.php):

we need to show all topic on a single place so that user can access all topic from one point.Here is the php code for this purpose :) .

<?php
$host=”localhost”; // Host name
$username=””; // Mysql username
$password=””; // Mysql password
$db_name=”test”; // Database name
$tbl_name=”forum_question”; // Table name

// Connect to server and select database.
mysql_connect(“$host”, “$username”, “$password”)or die(“cannot connect”);
mysql_select_db(“$db_name”)or die(“cannot select DB”);

$sql=”SELECT * FROM $tbl_name ORDER BY id DESC”;
// ORDER BY id DESC is order result by descending
$result=mysql_query($sql);
?>
<table width=”90%” border=”0″ align=”center” cellpadding=”3″ cellspacing=”1″ bgcolor=”#CCCCCC”>
<tr>
<td width=”6%” align=”center” bgcolor=”#E6E6E6″><strong>#</strong></td>
<td width=”53%” align=”center” bgcolor=”#E6E6E6″><strong>Topic</strong></td>
<td width=”15%” align=”center” bgcolor=”#E6E6E6″><strong>Views</strong></td>
<td width=”13%” align=”center” bgcolor=”#E6E6E6″><strong>Replies</strong></td>
<td width=”13%” align=”center” bgcolor=”#E6E6E6″><strong>Date/Time</strong></td>
</tr>
<?php
// Start looping table row
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td bgcolor=”#FFFFFF”><?php echo $rows[‘id’]; ?></td>
<td bgcolor=”#FFFFFF”><a href=”view_topic.php?id=<?php echo $rows[‘id’]; ?>”><?php echo $rows[‘topic’]; ?></a><BR></td>
<td align=”center” bgcolor=”#FFFFFF”><?php echo $rows[‘view’]; ?></td>
<td align=”center” bgcolor=”#FFFFFF”><?php echo $rows[‘reply’]; ?></td>
<td align=”center” bgcolor=”#FFFFFF”><?php echo $rows[‘datetime’]; ?></td>
</tr>

<?php
// Exit looping and close connection
}
mysql_close();
?>

<tr>
<td colspan=”5″ align=”right” bgcolor=”#E6E6E6″><a href=”create_topic.php”><strong>Create New Topic</strong> </a></td>
</tr>
</table>

Step 5 – Create Page to view topic as a whole (view_topic.php):

Before this we only create page to show all topic.What if we want to add answer or comment on some topic or want to see all discussion in a specific topic.For this purpose create view_topic.php page.Here is its code.

<?php

$host=”localhost”; // Host name
$username=””; // Mysql username
$password=””; // Mysql password
$db_name=”test”; // Database name
$tbl_name=”forum_question”; // Table name

// Connect to server and select database.
mysql_connect(“$host”, “$username”, “$password”)or die(“cannot connect”);
mysql_select_db(“$db_name”)or die(“cannot select DB”);

// get value of id that sent from address bar
$id=$_GET[‘id’];

$sql=”SELECT * FROM $tbl_name WHERE id=’$id'”;
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);
?>

<table width=”400″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”1″ bgcolor=”#CCCCCC”>
<tr>
<td><table width=”100%” border=”0″ cellpadding=”3″ cellspacing=”1″ bordercolor=”1″ bgcolor=”#FFFFFF”>
<tr>
<td bgcolor=”#F8F7F1″><strong><? echo $rows[‘topic’]; ?></strong></td>
</tr>

<tr>
<td bgcolor=”#F8F7F1″><? echo $rows[‘detail’]; ?></td>
</tr>

<tr>
<td bgcolor=”#F8F7F1″><strong>By :</strong> <? echo $rows[‘name’]; ?> <strong>Email : </strong><? echo $rows[’email’];?></td>
</tr>

<tr>
<td bgcolor=”#F8F7F1″><strong>Date/time : </strong><? echo $rows[‘datetime’]; ?></td>
</tr>
</table></td>
</tr>
</table>
<BR>

<?php

$tbl_name2=”forum_answer”; // Switch to table “forum_answer”

$sql2=”SELECT * FROM $tbl_name2 WHERE question_id=’$id'”;
$result2=mysql_query($sql2);

while($rows=mysql_fetch_array($result2)){
?>

<table width=”400″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”1″ bgcolor=”#CCCCCC”>
<tr>
<td><table width=”100%” border=”0″ cellpadding=”3″ cellspacing=”1″ bgcolor=”#FFFFFF”>
<tr>
<td bgcolor=”#F8F7F1″><strong>ID</strong></td>
<td bgcolor=”#F8F7F1″>:</td>
<td bgcolor=”#F8F7F1″><? echo $rows[‘a_id’]; ?></td>
</tr>
<tr>
<td width=”18%” bgcolor=”#F8F7F1″><strong>Name</strong></td>
<td width=”5%” bgcolor=”#F8F7F1″>:</td>
<td width=”77%” bgcolor=”#F8F7F1″><? echo $rows[‘a_name’]; ?></td>
</tr>
<tr>
<td bgcolor=”#F8F7F1″><strong>Email</strong></td>
<td bgcolor=”#F8F7F1″>:</td>
<td bgcolor=”#F8F7F1″><? echo $rows[‘a_email’]; ?></td>
</tr>
<tr>
<td bgcolor=”#F8F7F1″><strong>Answer</strong></td>
<td bgcolor=”#F8F7F1″>:</td>
<td bgcolor=”#F8F7F1″><? echo $rows[‘a_answer’]; ?></td>
</tr>
<tr>
<td bgcolor=”#F8F7F1″><strong>Date/Time</strong></td>
<td bgcolor=”#F8F7F1″>:</td>
<td bgcolor=”#F8F7F1″><? echo $rows[‘a_datetime’]; ?></td>
</tr>
</table></td>
</tr>
</table><br>
<?php
}
$sql3=”SELECT view FROM $tbl_name WHERE id=’$id'”;
$result3=mysql_query($sql3);

$rows=mysql_fetch_array($result3);
$view=$rows[‘view’];
// if have no counter value set counter = 1
if(empty($view)){
$view=1;
$sql4=”INSERT INTO $tbl_name(view) VALUES(‘$view’) WHERE id=’$id'”;
$result4=mysql_query($sql4);
}
// count more value
$addview=$view+1;
$sql5=”update $tbl_name set view=’$addview’ WHERE id=’$id'”;
$result5=mysql_query($sql5);
mysql_close();
?>

<BR>
<table width=”400″ border=”0″ align=”center” cellpadding=”0″ cellspacing=”1″ bgcolor=”#CCCCCC”>
<tr>
<form name=”form1″ method=”post” action=”add_answer.php”>
<td>
<table width=”100%” border=”0″ cellpadding=”3″ cellspacing=”1″ bgcolor=”#FFFFFF”>
<tr>
<td width=”18%”><strong>Name</strong></td>
<td width=”3%”>:</td>
<td width=”79%”><input name=”a_name” type=”text” id=”a_name” size=”45″></td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td>:</td>
<td><input name=”a_email” type=”text” id=”a_email” size=”45″></td>
</tr>
<tr>
<td valign=”top”><strong>Answer</strong></td>
<td valign=”top”>:</td>
<td><textarea name=”a_answer” cols=”45″ rows=”3″ id=”a_answer”></textarea></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name=”id” type=”hidden” value=”<? echo $id; ?>”></td>
<td><input type=”submit” name=”Submit” value=”Submit”> <input type=”reset” name=”Submit2″ value=”Reset”></td>
</tr>
</table>
</td>
</form>
</tr>
</table>

Step 6 – Final Step add page to insert Answer (add_answer.php):

this is the final and last step here we make a page which is used to add the comment or replies on the forum or discussion board.Answer form will look like this

how to create simple php forum or Discussion Board [howpk.com]

how to create simple php forum or Discussion Board [howpk.com]

Here is the code. :)

<?php

$host=”localhost”; // Host name
$username=””; // Mysql username
$password=””; // MySQL password
$db_name=”test”; // Database name
$tbl_name=”forum_answer”; // Table name

// Connect to server and select database.
mysql_connect(“$host”, “$username”, “$password”)or die(“cannot connect”);
mysql_select_db(“$db_name”)or die(“cannot select DB”);

// Get value of id that sent from hidden field
$id=$_POST[‘id’];

// Find highest answer number.
$sql=”SELECT MAX(a_id) AS Maxa_id FROM $tbl_name WHERE question_id=’$id'”;
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);

// add + 1 to highest answer number and keep it in variable name “$Max_id”. if there no answer yet set it = 1
if ($rows) {
$Max_id = $rows[‘Maxa_id’]+1;
}
else {
$Max_id = 1;
}

// get values that sent from form
$a_name=$_POST[‘a_name’];
$a_email=$_POST[‘a_email’];
$a_answer=$_POST[‘a_answer’];

$datetime=date(“d/m/y H:i:s”); // create date and time


// Insert answer

$sql2=”INSERT INTO $tbl_name(question_id, a_id, a_name, a_email, a_answer, a_datetime)VALUES(‘$id’, ‘$Max_id’, ‘$a_name’, ‘$a_email’, ‘$a_answer’, ‘$datetime’)”;
$result2=mysql_query($sql2);

if($result2){
echo “Successful<BR>”;
echo “<a href=’view_topic.php?id=”.$id.”‘>View your answer</a>”;


// If added new answer, add value +1 in reply column

$tbl_name2=”forum_question”;
$sql3=”UPDATE $tbl_name2 SET reply=’$Max_id’ WHERE id=’$id'”;
$result3=mysql_query($sql3);

}
else {
echo “ERROR”;
}

// Close connection

mysql_close();
?>

So what you think i missed ? I hope the article of how to create simple php forum or Discussion Board is very useful for PHP developer.If you have any question and face any problem regarding this article feel free to comment us.you may like to read How to create a simple website in PHP tutorial class 1.

i recommended you to must read How to Delete Pages and Menu from PHP admin Panel. and how to create menu in php Dynamically – php navigation.

Author: Tanvir Zafar

Tanvir Zafar is a internet Entrepreneur and owner of this site and many others as well. He is student in GCUF doing BS Software Engineering. :)