Im going to use MySQL for this tutorial.
First create a database. Im going to use one i call "Test_Forum".
Second we're gonna create the tables:
SQL: (for execution in PHPMyAdmin)
CODE
CREATE TABLE `topics` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`title` VARCHAR( 255 ) NOT NULL ,
`post` VARCHAR( 255 ) NOT NULL ,
`author` VARCHAR( 255 ) NOT NULL ,
`mail` VARCHAR( 255 ) NOT NULL ,
`date` VARCHAR( 255 ) NOT NULL
) ENGINE = MYISAM;
CREATE TABLE `replies` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`topic_id` INT( 11 ) NOT NULL ,
`post` VARCHAR( 255 ) NOT NULL ,
`author` VARCHAR( 255 ) NOT NULL ,
`mail` VARCHAR( 255 ) NOT NULL ,
`date` VARCHAR( 255 ) NOT NULL
) ENGINE = MYISAM;
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`title` VARCHAR( 255 ) NOT NULL ,
`post` VARCHAR( 255 ) NOT NULL ,
`author` VARCHAR( 255 ) NOT NULL ,
`mail` VARCHAR( 255 ) NOT NULL ,
`date` VARCHAR( 255 ) NOT NULL
) ENGINE = MYISAM;
CREATE TABLE `replies` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`topic_id` INT( 11 ) NOT NULL ,
`post` VARCHAR( 255 ) NOT NULL ,
`author` VARCHAR( 255 ) NOT NULL ,
`mail` VARCHAR( 255 ) NOT NULL ,
`date` VARCHAR( 255 ) NOT NULL
) ENGINE = MYISAM;
Step 2: index.php
Now we're gonna make the index.php to show our topics.
CODE
<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "Test_Forum";
$con = mysql_connect($host,$user,$pass);
mysql_select_db($db,$con);
echo "<a href='create_topic.php'>New topic</a>";
echo "<table border='0' width='100%'>
<tr><td>#</td><td>Title</td><td>Replies</td><td>Post Date</td></tr>";
$sql = mysql_query("SELECT * FROM topics ORDER BY id DESC");
while($row = mysql_fetch_array($sql)) {
$replies = mysql_num_rows(mysql_query("SELECT * FROM replies WHERE topic_id='".$row['id']."'"));
echo "<tr><td>".$row['id']."</td><td><a href='show_topic.php?id=".$row['id']."'>".htmlentities($row['title'])."</a></td><td>".$replies."</td><td>".date("j/n - y",$row['date'])."</td></tr>";
}
echo "</table>";
mysql_close($con);
?>
$host = "localhost";
$user = "root";
$pass = "";
$db = "Test_Forum";
$con = mysql_connect($host,$user,$pass);
mysql_select_db($db,$con);
echo "<a href='create_topic.php'>New topic</a>";
echo "<table border='0' width='100%'>
<tr><td>#</td><td>Title</td><td>Replies</td><td>Post Date</td></tr>";
$sql = mysql_query("SELECT * FROM topics ORDER BY id DESC");
while($row = mysql_fetch_array($sql)) {
$replies = mysql_num_rows(mysql_query("SELECT * FROM replies WHERE topic_id='".$row['id']."'"));
echo "<tr><td>".$row['id']."</td><td><a href='show_topic.php?id=".$row['id']."'>".htmlentities($row['title'])."</a></td><td>".$replies."</td><td>".date("j/n - y",$row['date'])."</td></tr>";
}
echo "</table>";
mysql_close($con);
?>
Explanation:
First we connect to the host and database using the functions mysql_connect() and mysql_select_db().
Then we make the link to create_topic.php.
Then we make the start of a table where we will inside of it save our info.
The $sql variable contains the mysql_query and SQL to use in our while loop.
Inside the while loop we got a $row variable with a function called mysql_fetch_array() which uses the $sql variable.
The $replies variable counts the number of rows inside the replies table using the functions mysql_num_rows().
Then we post the HTML with the $row array.
And lastly we close the connection to the MySQL host using mysql_close().
Step 3: create_topic.php
This is gonna exist of a form which leads us to add_topic.php.
CODE
<table>
<form action='add_topic.php' method='post'>
<tr><td>Name</td><td>:</td><td><input type='text' id='name' name='name' /></td></tr>
<tr><td>Email</td><td>:</td><td><input type='text' id='mail' name='mail' /></td></tr>
<tr><td>Title</td><td>:</td><td><input type='text' id='title' name='title' /></td></tr>
<tr><td valign='top'>Post</td><td valign='top'>:</td><td><textarea id='post' name='post' rows='7'></textarea></td></tr>
<tr><td> </td><td> </td><td><input type='submit' value='Post Topic' /> <input type='reset' value='Reset Fields' /></td></tr>
</form>
</table>
<form action='add_topic.php' method='post'>
<tr><td>Name</td><td>:</td><td><input type='text' id='name' name='name' /></td></tr>
<tr><td>Email</td><td>:</td><td><input type='text' id='mail' name='mail' /></td></tr>
<tr><td>Title</td><td>:</td><td><input type='text' id='title' name='title' /></td></tr>
<tr><td valign='top'>Post</td><td valign='top'>:</td><td><textarea id='post' name='post' rows='7'></textarea></td></tr>
<tr><td> </td><td> </td><td><input type='submit' value='Post Topic' /> <input type='reset' value='Reset Fields' /></td></tr>
</form>
</table>
This form is self-explanatory.
Step 4: add_topic.php
CODE
<?php
if (!empty($_POST)) {
$host = "localhost";
$user = "root";
$pass = "";
$db = "Test_Forum";
$con = mysql_connect($host,$user,$pass);
mysql_select_db($db,$con);
if (mysql_query("INSERT INTO topics (title,post,author,mail,date) VALUES ('".$_POST['title']."','".$_POST['post']."','".$_POST['name']."','".$_POST['mail']."','".time()."')")) {
header("location:index.php");
} else {
echo "Error!";
}
mysql_close($con);
}
?>
if (!empty($_POST)) {
$host = "localhost";
$user = "root";
$pass = "";
$db = "Test_Forum";
$con = mysql_connect($host,$user,$pass);
mysql_select_db($db,$con);
if (mysql_query("INSERT INTO topics (title,post,author,mail,date) VALUES ('".$_POST['title']."','".$_POST['post']."','".$_POST['name']."','".$_POST['mail']."','".time()."')")) {
header("location:index.php");
} else {
echo "Error!";
}
mysql_close($con);
}
?>
First we connect to the host and database using the functions mysql_connect() and mysql_select_db().
Then we use mysql_query to insert the data into our table.
If its a succes it will return you to index.php.
If not... Well it will say Error...
Step 5: show_topic.php
CODE
<?php
if (isset($_GET['id'])){
$host = "localhost";
$user = "root";
$pass = "";
$db = "Test_Forum";
$con = mysql_connect($host,$user,$pass);
mysql_select_db($db,$con);
$id = $_GET['id'];
echo "<table border='0' width='100%'>";
$sql = mysql_query("SELECT * FROM topics WHERE id='".$id."'");
while($row = mysql_fetch_array($sql)) {
echo "<tr><td>".htmlentities($row['title'])."</td></tr>
<tr><td>".str_replace("\n","<br />",htmlentities($row['post']))."</td></tr>
<tr><td><a href='mailto:".$row['mail']."'>".htmlentities($row['author'])."</a></td></tr>
<tr><td>".date("j/n - y",$row['date'])."</td></tr>";
}
echo "<tr><td> </td></tr>";
$sql2 = mysql_query("SELECT * FROM replies WHERE topic_id='".$id."' ORDER BY id ASC");
while($row2 = mysql_fetch_array($sql2)) {
echo "<tr><td>".str_replace("\n","<br />",htmlentities($row2['post']))."</td></tr>
<tr><td><a href='mailto:".$row2['mail']."'>".htmlentities($row2['author'])."</a></td></tr>
<tr><td>".date("j/n - y",$row2['date'])."</td></tr>";
}
echo "<tr><td> </td></tr>";
echo "<tr><td>
<table border='0' width='100%'>
<form action='add_reply.php' method='post'>
<input type='hidden' id='topic_id' name='topic_id' value='".$id."' />
<tr><td>Name</td><td>:</td><td><input type='text' id='name' name='name' /></td></tr>
<tr><td>Mail</td><td>:</td><td><input type='text' id='mail' name='mail' /></td></tr>
<tr><td valign='top'>Post</td><td>:</td><td><textarea id='post' name='post' rows='7'></textarea></td></tr>
<tr><td> </td><td> </td><td><input type='submit' value='Post Reply' /> <input type='reset' value='Reset Fields' /></td></tr>
</form>
</table>
</td></tr>";
echo "</table>";
mysql_close($con);
} else {
echo "invalid usage!";
}
?>
if (isset($_GET['id'])){
$host = "localhost";
$user = "root";
$pass = "";
$db = "Test_Forum";
$con = mysql_connect($host,$user,$pass);
mysql_select_db($db,$con);
$id = $_GET['id'];
echo "<table border='0' width='100%'>";
$sql = mysql_query("SELECT * FROM topics WHERE id='".$id."'");
while($row = mysql_fetch_array($sql)) {
echo "<tr><td>".htmlentities($row['title'])."</td></tr>
<tr><td>".str_replace("\n","<br />",htmlentities($row['post']))."</td></tr>
<tr><td><a href='mailto:".$row['mail']."'>".htmlentities($row['author'])."</a></td></tr>
<tr><td>".date("j/n - y",$row['date'])."</td></tr>";
}
echo "<tr><td> </td></tr>";
$sql2 = mysql_query("SELECT * FROM replies WHERE topic_id='".$id."' ORDER BY id ASC");
while($row2 = mysql_fetch_array($sql2)) {
echo "<tr><td>".str_replace("\n","<br />",htmlentities($row2['post']))."</td></tr>
<tr><td><a href='mailto:".$row2['mail']."'>".htmlentities($row2['author'])."</a></td></tr>
<tr><td>".date("j/n - y",$row2['date'])."</td></tr>";
}
echo "<tr><td> </td></tr>";
echo "<tr><td>
<table border='0' width='100%'>
<form action='add_reply.php' method='post'>
<input type='hidden' id='topic_id' name='topic_id' value='".$id."' />
<tr><td>Name</td><td>:</td><td><input type='text' id='name' name='name' /></td></tr>
<tr><td>Mail</td><td>:</td><td><input type='text' id='mail' name='mail' /></td></tr>
<tr><td valign='top'>Post</td><td>:</td><td><textarea id='post' name='post' rows='7'></textarea></td></tr>
<tr><td> </td><td> </td><td><input type='submit' value='Post Reply' /> <input type='reset' value='Reset Fields' /></td></tr>
</form>
</table>
</td></tr>";
echo "</table>";
mysql_close($con);
} else {
echo "invalid usage!";
}
?>
First we check if ID is set.
If it is we will connect to the host and database using the functions mysql_connect() and mysql_select_db().
And then we set the $id variable to $_GET['id'] from the address bar.
And then we set the $sql variable to contain the mysql_query and SQL to use in our while loop.
And then inside the while loop we got a $row variable with a function called mysql_fetch_array() which uses the $sql variable.
This is the Topic.
Then we make a seperation cell outside of the loop.
And then we set the $sql2 variable to contain the mysql_query and SQL to use in our while loop.
And then inside the while loop we got a $row2 variable with a function called mysql_fetch_array() which uses the $sql2 variable.
These are the replies if there are any.
Then we make the form to submit replies.
It leads to add_reply.php.
Then we close table and mysql connection.
Step 6: add_reply.php
CODE
<?php
if (!empty($_POST)) {
$host = "localhost";
$user = "root";
$pass = "";
$db = "Test_Forum";
$con = mysql_connect($host,$user,$pass);
mysql_select_db($db,$con);
if (mysql_query("INSERT INTO replies (topic_id,post,author,mail,date) VALUES ('".$_POST['topic_id']."','".$_POST['post']."','".$_POST['name']."','".$_POST['mail']."','".time()."')")) {
header("location:showtopic.php?id=".$_POST['topic_id']);
} else {
echo "Error!";
}
mysql_close($con);
} else {
echo "Invalid usage!";
}
?>
if (!empty($_POST)) {
$host = "localhost";
$user = "root";
$pass = "";
$db = "Test_Forum";
$con = mysql_connect($host,$user,$pass);
mysql_select_db($db,$con);
if (mysql_query("INSERT INTO replies (topic_id,post,author,mail,date) VALUES ('".$_POST['topic_id']."','".$_POST['post']."','".$_POST['name']."','".$_POST['mail']."','".time()."')")) {
header("location:showtopic.php?id=".$_POST['topic_id']);
} else {
echo "Error!";
}
mysql_close($con);
} else {
echo "Invalid usage!";
}
?>
First we connect to the host and database using the functions mysql_connect() and mysql_select_db() if $_POST is not empty.
Then we use mysql_query to insert the data into our table.
If its a succes it will return you to the topic.
If not... Well it will say Error...
Step 7: Finish!
This concludes the simple forum tutorial.
Hope you find it useful.
I have not made any edit or delete because its a simple forum.
This has been tested on a PHP server and it works.
Online Demo
