PHP Mini Message Board Tutorial
Mini Message Board Tutorial
Many websites like to have a message board / forum on their site, but would prefer their own "product". Well if you like, feel free to follow this tutorial and you'll learn how to create a mini message board in PHP and MySQL.Database Stuff
Before we start with any of the coding, you will need to go into your phpMyAdmin and (if you haven't already got a 'threads' and 'replies' table) paste this in the SQL box. If you already have a threads and replies table in your database then you will need to change it accordingly to what's on the tutorial.This will allow the information to be stored in the database.CREATE TABLE `threads` ( `id` INT NOT NULL AUTO_INCREMENT , `title` VARCHAR( 255 ) NOT NULL , `message` TEXT NOT NULL , `author` VARCHAR( 255 ) NOT NULL , `replies` INT( 11 ) NOT NULL , `posted` VARCHAR( 255 ) NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM; CREATE TABLE `replies` ( `id` INT NOT NULL AUTO_INCREMENT , `thread` INT( 11 ) NOT NULL , `message` TEXT NOT NULL , `author` VARCHAR( 255 ) NOT NULL , `posted` VARCHAR( 255 ) NOT NULL , PRIMARY KEY ( `id` ) ) ENGINE = MYISAM
New Thread Form
Now as that has been added we need to start with the first page (index.php).The code above has connected to the database. You will need to change USERNAME to your database username, PASSWORD to your database password and DATABASE to the name of your database.<?php mysql_connect("localhost", "USERNAME", "PASSWORD"); mysql_select_db("DATABASE"); ?>
Now we need to first of all be able to make a thread. To do this you'll need to create a form on the index.php page with three boxes and a button.
Your page should look something like this at the moment.<form action="newthread.php" method="POST"> Your Name: <input type="text" name="author"><br> Thread Title: <input type="text" name="title"><br> Thread:<br><textarea cols="60" rows="5" name="message"></textarea><br> <input type="submit" value="Post Thread"> </form>

Inserting the New Thread
Now we need the form to enter the information into the database. To do this we will create a file, which is where the form will send the data to called 'newthread.php'. At the top of the file you will need to connect to the database again, then add an INSERT function.If you don't understand what's happening about, basically we're telling the script to add the data sent from the form on the previous page into the database.<?php mysql_connect("localhost", "USERNAME", "PASSWORD"); mysql_select_db("DATABASE"); $time = time(); mysql_query("INSERT INTO threads VALUES(NULL,'$_POST[title]','$_POST[message]','$_POST[author]','0','$time')"); echo "Thread Posted.<br><a href='index.php'>Return</a>"; ?>
Hopefully when you fill something in the form and click "Post Thread" you should get a message like this.

Showing List of Threads
Great! So far. You've been able to add a thread into the database. Now we need to be able to show this thread. To do this we will be using mysql_query() again and also mysql_fetch_array().On index.php underneath the end of the form (</form>) add a horizontal rule (<hr>) then add this PHP beneath it. I will explain it all with comments within the code.
If you have followed the tutorial correctly you should now see something like this.<?php // We are selecting everything from the threads section in the database and ordering them newest to oldest. $sql = mysql_query("SELECT * FROM threads ORDER BY posted DESC"); // Now we are getting our results and making them an array while($r = mysql_fetch_array($sql)) { // Everything within the two curly brackets can read from the database using $r[] // We need to convert the UNIX Timestamp entered into the database for when a thread... // ... is posted into a readable date, using date(). $posted = date("jS M Y h:i",$r[posted]); // Now we will show the available threads echo "<h3><a href='msg.php?id=$r[id]'>$r[title]</a> ($r[replies])</h3><h4>Posted by $r[author] on $posted</h4>"; // End of Array } ?>

Reading Threads
Basically everything on the index page is done now. All that's left now is reading a thread and replying to a thread.This stage will teach you how to read the threads. Create a file called 'msg.php'
Connect to the database...
Add a link to the previous page, so you can go back to the index page.<?php // Connecting to the database again mysql_connect("localhost", "USERNAME", "PASSWORD"); mysql_select_db("DATABASE");
Do a mysql_query to select the thread you're reading.// Here's a link that will allow you to go back to the index echo "<a href='index.php'>Go Back...</a>";
Show the result from the thread query and show it with a horizontal rule beneath it.// This query selects the current thread using the $_GET value. $sql = mysql_query("SELECT * FROM threads WHERE id = '$_GET[id]'");
Now to show the replies, using the $_GET query from the thread.// Now we are getting our results and making them an array while($r = mysql_fetch_array($sql)) { // Here is the thread title. echo "<h2>$r[title]</h2>"; // Everything within the two curly brackets can read from the database using $r[] // We need to convert the UNIX Timestamp entered into the database for when a thread... // ... is posted into a readable date, using date(). $posted = date("jS M Y h:i",$r[posted]); // Now this shows the thread with a horizontal rule after it. echo "$r[message]<h4>Posted by $r[author] on $posted</h4><hr>"; // End of Array }
Now show the form to do the reply in.echo "<h3>Replies...</h3>"; // Here we will get it to show the replies // This query selects the replies from the database where the thread ID matches the thread $_GET value. $sql = mysql_query("SELECT * FROM replies WHERE thread = '$_GET[id]'"); // Now we are getting our results and making them an array while($r = mysql_fetch_array($sql)) { // Everything within the two curly brackets can read from the database using $r[] // We need to convert the UNIX Timestamp entered into the database for when a thread... // ... is posted into a readable date, using date(). $posted = date("jS M Y h:i",$r[posted]); // Now this shows the thread with a horizontal rule after it. echo "$r[message]<h4>Posted by $r[author] on $posted</h4><hr>"; // End of Array } ?>
This will now allow you to do a reply to the thread. However if you test it you will get an Error 404 (cannot find the file), so now we'll need to make the 'newreply.php' file.<form action="newreply.php" method="POST"> Your Name: <input type="text" name="author"> <input type="hidden" value="<?php echo $_GET[id]; ?>" name="thread"><br> Message:<br><textarea cols="60" rows="5" name="message"></textarea><br> <input type="submit" value="Post Reply"> </form>
Inserting the Reply
Connect to the database again.Get the current time as a UNIX Timestamp as a varible.<?php mysql_connect("localhost", "USERNAME", "PASSWORD"); mysql_select_db("DATABASE");
Insert the information sent from the form into the database.$time = time();
Now we will need to update the reply count in the threads database.mysql_query("INSERT INTO replies VALUES(NULL,'$_POST[thread]','$_POST[message]','$_POST[author]','$time')");
Now make a little 'reply posted' message with a link back to the thread.mysql_query("UPDATE threads SET replies = replies + 1 WHERE id = '$_POST[thread]'");
Give it a try and it should send a reply to the thread.echo "Reply Posted.<br><a href='msg.php?id=$_POST[thread]'>Return</a>"; ?>
Finished
That's basically it. Obviously this is a quick and dirty method of doing a message board and it is open to so many bugs, hacks, XSS, etc.. but the code can be optimized to make it better.What you should have learned...
- How to build a HTML form
- How to connect to a database
- How to read information from a database
- How to add new information into the database
Things to make it better?
- Why not try adding a registration section on it, that will allow visitors to sign up and post without having to enter their name each time.
- Add an post icons for each post, so posters can select various icons for each topic.
- Create an Admin Control Panel, for ease of use when you need to remove a reply or thread.
- Add more security to the board. (Search around about strip_slashes(), html_special_chars() and similar things)
- Use nl2br() to make each line down show as a new line down.
- Adapt on the methods used in this tutorial to make a discussion board / forum with different rooms and categories
Feedback
I'd love to hear your feedback. If you have any then please contact Dale Hay (that's me!) or leave a comment on this post.