Create a website RSS feed

Create Your Forum With Php

create your forum with php

Last update on: 10-27-2008
Yes! we will now create something better it is a mini forum application, thats has a subject and response submit.

Just to remind you, try to not copy and paste the code.
Ok! lets go

Create the mysql table:

#
#structure of the mysql forum table
#
CREATE TABLE forum_tbl (
id int NOT NULL auto_increment,
response_id varchar (30) not null,
date varchar(20) NOT NULL,
username varchar (50) NOT NULL,
email varchar(55) NOT NULL,
subject varchar (60) NOT NULL,
message text NOT NULL,
PRIMARY KEY (id)
);

The parameters that we gave to our mysql table are, the id of the post, response_id will record the id of the post that get response, date of the post, username of the poster, his/her email, the post subject, and the message content of the post.

Ok! next like every time we need a form to submit the posts, lets name it (new.php).

<form method="post" action="add.php">
<table style="width:350px; margin-left:auto; margin-right:auto">
<tr>
<td style="width:100px"><strong>UserName</strong></td>
<td style="width:250px"><input type="text" name="username" /></td>
</tr>
<tr>
<td style="width:100px"><strong>E-Mail</strong></td>
<td style="width:250px"><input type="text" name="email" /></td>
</tr>
<td style="width:100px"><strong>Subject</strong></td>
<td style="width:250px"><input type="text" name="subject" /></td>
</tr>
<td colspan="2" style="text-align:center;font-weight:bold">Message</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<textarea name="message" wrap="VIRTUAL" cols="40" rows="10"></textarea>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center"><input type="submit" name="submit" value="Add the post" /></td>
</tr>
</table>
</form>

I think every thing is clear here, just I have to mention one thing is that we named the page as (new.php) and we did not use the html opening and closing tags, just because we will use the include statement to include this page.

Ok! we create our post submit page, now we need the response page form, lets give it a name of (response.php).

You can see on the first form we didn't use the response_id as a field thats because we will use it only when someone want to response to a subject, so the response record will record the subject id on the response column.

<form method="post" action="add.php">
<!-- we have to add on the next line the associate post for the response_id...-->
<!--like this for each (subject) post will have it or its correspond response_id-->

<input type="hidden" name="response_id" value="<?php print $_GET['id']; ?>" />
<table style="width:350px;margin-left:auto;margin-right:auto">
<tr>
<td style="width:100px;font-weight:bold">UserName</td>
<td style="width:250px"><input type="text" name="username" /></td>
</tr>
<tr>
<td style="width:100px; font-weight:bold">E-Mail</td>
<td style="width:250px"><input type="text" name="email" /></td>
</tr>
<td style="width:100px; font-weight:bold">Subject</td>
<td style="width:250px"><input type="text" name="subject" /></td>
</tr>
<td colspan="2" style="text-align:center; font-weight:bold ">Message</td>
</tr>
<tr>
<td colspan="2" style="text-align:center">
<textarea name="message" wrap="VIRTUAL" cols="40" rows="10"></textarea>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center"><input type="submit" name="submit" value="Send response" /></td>
</tr>
</table>
</form>
Don't forget the hidden field (response_id), it's very important, without it, we will not know if it is a response or a new subject.

Now we need the add.php page that will process our forms.

<?php
//----------------------------------------------------------------
//
// add.php page
//
// will add messages and responses to the data base
// verify the submited data is very important in this case
//
//----------------------------------------------------------------
$mysql_host = 'localhost'; // Host name
$mysql_user = 'your login'; // Your login
$mysql_password = 'password'; // Your password
$mysql_base = 'data base name'; // enter the data base name
//----------------------------------------------------------------

// first we need to verify field submitted by the forms
// If the fields username ,subject or message are empty
if( empty( $_POST['username']) || empty($_POST['subject']) || empty($_POST['message']) )
{

print
'<a href="javascript:history.back();">Ooops! Go back to complet the form</a>';
}
else{ // if not! everything is ok and move on

//we set the date for the output order
$date = date("m-d-Y H:i");

// removing the special characters
$username = AddSlashes (htmlspecialchars($_POST['username']));
$email = AddSlashes (htmlspecialchars($_POST['email']));
$message = AddSlashes (htmlspecialchars($_POST['message']));

//Connect to the data base server
$db = mysql_connect($mysql_host,$mysql_user,$mysql_password) or die ("server connexion error");

//we select the mysql data base
mysql_select_db($mysql_base,$db) or die ("data base connexion error");

//the request to insert the data
mysql_query("INSERT Into forum_tbl (id,response_id,date,username,email,subject,message) VALUES ('','$_POST[response_id]','$date','$username','$email','$subject','$message') ") or die ("request error". mysql_error());

//close the connexion
mysql_close();

//send the visitor to the home page, where the messages will be displaying (index.php).
header('location: index.php');
}
?>

Ok! lets create our forum home page (index.php).

<?php
//----------------------------------------------------------------
//
// subjects, authors, and dates displaying page
// index.php
//
//----------------------------------------------------------------
$mysql_host = 'localhost'; // Host name
$mysql_user = 'your login'; // Your login
$mysql_password = 'password'; // Your password
$mysql_base = 'data base name'; // enter the data base name
//----------------------------------------------------------------

//Connect to the data base server
$db = mysql_connect($mysql_host,$mysql_user,$mysql_password) or die ("server connexion error");

//we select the mysql data base
mysql_select_db($mysql_base,$db) or die ("data base connexion error");

//the request to select the messages, note that I sued the clause where and I limied the output message to 50...
//use the muliple pages lesson to divide your messages into pages
//we select only the record with an empty response_id
$req = mysql_query("SELECT id,date,username,email,subject from forum_tbl where response_id ='' Order by date Desc Limit 0, 50 ") or die ("request error");

//we use a loop to get all the selected records, and we stock them into an array with mysql_fetch_array()
while( $forum = mysql_fetch_array ($req))
{

//we display the result as a link to the subject and its content
//we attach the id of the subject with the URL to a new page named display.php
print '<a href="display.php?id='.$forum['id'].'">'.$forum['subject'].'</a> -Author : <a href="mailto:'.$forum['email'].'">'.$forum['username'].'</a> posted on : '.$forum['date'].'<br />';
print
'<hr />';

}

//we close the connexion
mysql_close();

include('new.php'); // we include the from (new.php) at the bottom.
//----------------- end of the index.php page--------------------------------

?>

I have used a basic html tags to print the subjects but you can make it more attractive if you want.

Now the displaying page of the messages with their responses: (display.php)

<?php
//----------------------------------------------------------------
//
// messages and responses displaying page
// display.php
//
//----------------------------------------------------------------
$mysql_host = 'localhost'; // Host name
$mysql_user = 'your login'; // Your login
$mysql_password = 'password'; // Your password
$mysql_base = 'data base name'; // enter the data base name
//----------------------------------------------------------------

//Connect to the data base server
$db = mysql_connect($mysql_host,$mysql_user,$mysql_password) or die ("server connexion error");

//we select the mysql data base
mysql_select_db($mysql_base,$db) or die ("data base connexion error");

//----------------------------------------------------------------
//
// we said that we attached the response_id with the links that will...
//
//----------------------------------------------------------------
$req = mysql_query("SELECT id,date,username,email,subject,message from forum_tbl where id ='".$_GET['id']."' ") or die ("request error");

//we use a loop to get all the selected records, and...
//we stock them into an array with mysql_fetch_array()
$subj_det = mysql_fetch_array ($req);

//we display the full message of the subject
print $subj_det[sujet].'<br / >';
print
'author : <a href="mailto:'.$subj_det['email'].'">'.$subj_det['username'].'</a> posted on : '.$subj_det['date'].'<br />';
print
'message: <br />';
print
$subj_det['message'];

print
'<hr />'; // a separation line between the message and its responses

//----------------------------------------------------------------
//
// display the messages and their responses
// //----------------------------------------------------------------
$rep = mysql_query("SELECT id,date,username,email,sujet,message from forum_tbl where reponse_id ='".$_GET['id']."' ") or die ("request response error ");

while ( $response = mysql_fetch_array ($rep)){

// we display the responses
print $reponse['sujet'].'<br />';
print
'Author : <a href="mailto:'.$response['email'].'">'.$response['username'].'</a> posted on : '.$response['date'].'<br />';
print
'message: <br />';
print
$response['message'];
print
'<hr />'; // separate with a line between responses
}
//we close the connexion
mysql_close();

include('response.php'); //including the response form page (response.php)
//----------------- End Of The page--------------------------------

?>

The displaying is simple too, you can change is by adding others tags or styles. Remember that we included the response form at the bottom of the displaying page and that form has the hidden response_id that will be taken the value of the subject id selected, so any response will be automatically associated with its message.

So we done! it's a new script that seem to be basic but easy to manipulate and make it unique for you website.

No Copy Paste!


PHP and MySQL's lessons:

Introduction To PHP
Get Started With PHP
PHP Variables
PHP Variables Of Environment
PHP Conditions
PHP Looping
PHP Cookies
PHP Working With Dates
PHP Working With Arrays
PHP Working With Files
PHP Play With Strings
PHP And Forms
Send Emails With PHP
The Include Statement
Get Started With MySQL
MySQL Update And Delete
The WHERE Clause
MySQL Functions
Guestbook Script
Websites Directory Script
Multiple Pages With PHP
Create Your Forum With Php

Banner HomeServices Contact |  ©2009 http://www.iteachweb.net/