Honesty Rocks! truth rules.

How To: Make A Simple Php Forum System

HOME      >>       Programming


One of my first ever projects I embarked on when I began to understand PHP well was a forum system. I've decided to begin writing a tutorial to help people start one of their own. The code for this project is based on that of OakumBoard/cBoard, my own forum software which can be seen running at http://www.sonicxtremegm.co.cc/

Anyways, time to start off, you must run this SQL query on an SQL database with a name of your choice:

sCREATE TABLE `forums` ( `id` int(3) NOT NULL auto_increment, `name` varchar(80) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3;INSERT INTO `forums` (`id`, `name`) VALUES(1, 'Test Forum'),CREATE TABLE `members` ( `id` int(4) NOT NULL auto_increment, `username` varchar(65) NOT NULL default '', `password` varchar(65) NOT NULL default '', PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3;CREATE TABLE `replies` ( `topicid` int(4) NOT NULL, `reply_id` int(4) NOT NULL, `reply_user` varchar(50) NOT NULL, `reply_text` longtext NOT NULL, `reply_datetime` varchar(25) NOT NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1;CREATE TABLE `topics` ( `id` tinyint(4) NOT NULL auto_increment, `title` varchar(180) NOT NULL, `text` longtext NOT NULL, `datetime` varchar(30) NOT NULL default '0', `user` varchar(80) NOT NULL, `reply` int(4) NOT NULL, `forumid` int(3) NOT NULL, PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=6;

Now, you create a file called conf.php. Inside it should be the text:
<?php$host= "[b]localhost[/b]"; // Host name $username="[b]admin[/b]"; // Mysql username $password="[b]adminone[/b]"; // Mysql password $db_name= "[b]admindb[/b]"; // Database name mysql_connect("$host", "$username", "$password")or die("Failed to connect to DB server"); mysql_select_db("$db_name")or die("Failed to select DB");?>

Replace the bolded areas with the appriate value for your data.


Sorry for the contradiction, Miles. I would not say that this is a tutorial : this post shows the very nice program you made, it does not teach php.However, I find your php-by-example approach very constructive. That's why I moved your topic here, where is most probably it's place.RegardsYordan


I don't think you are done with this script or :S?Because the only thing you have here is the MySQL table, and the database-connection file..


Haven't finished writing it out yet. By the way, there is more below.
You are now going to create the main files: index.php. This contains nearly all the coding for the forum. You will place the following within it:

<?php$name = $_COOKIE['username_cb'];if ( $name == "" ) {$name="Guest";}//---------------------------------------------------------// Include all necessary files and find the requested page//---------------------------------------------------------include "conf.php";//---------------------------------------------------------// Prepare a few things//---------------------------------------------------------$page = $_GET['p']; if ($page==""){$page = main;}switch ($page) {//---------------------------------------------------------// Show the list of forums//---------------------------------------------------------case "showforums":echo "<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=\"76%\" align=\"center\" bgcolor=\"#E6E6E6\"><strong>Forum</strong></td><td width=\"18%\" align=\"center\" bgcolor=\"#E6E6E6\"><strong>Last Topic</strong></td></tr> ";$sql="SELECT * FROM forums ORDER BY id ASC";// Now to get the forums list done$result=mysql_query($sql);while($rows=mysql_fetch_array($result)){ echo "<tr><td bgcolor=\"#FFFFFF\">".$rows['id']."</td><td align=\"left\" valign = \"top\" bgcolor=\"#FFFFFF\"><B><a href=\"index.php?p=showtopics&id=". $rows['id'] ."\">". $rows['name'] ."</a></b><BR>". $rows['desc'] ."</td><td bgcolor=\"#FFFFFF\">".$rows['lastpost']."</td>"; }//Join it all upecho "</table>";break;}?>
More parts of this tutorial coming soon!


Forums are often very large and require many pages. Most forum softwares take a long time to make. The one I made however only was a few PHP pages and already out ranks every other one I have seen but the method that you are using will take a lot more space then mine did. I would recommend that you finish your entire tutorial on one post and rather then posting code just make a .txt file, upload it and link to it. This would really help people follow your post's easier. Your post could be a good tutorial if you were to put all the code together but at the moment all I see is a good tutorial in the making that isn't quite ready to be posted yet.Good luck,Sparkx



I need a bit help cause I get a notice error:

Notice: Undefined index: username_cb in /www/zxq.Net/t/e/s/test-site/htdocs/index.Php on line 2 Warning: include(conf.Php): failed to open stream: No such file or directory in /www/zxq.Net/t/e/s/test-site/htdocs/index.Php on line 9 Warning: include(): Failed opening 'conf.Php' for inclusion (include_path='.:/usr/lib/php') in /www/zxq.Net/t/e/s/test-site/htdocs/index.Php on line 9 Notice: Undefined index: p in /www/zxq.Net/t/e/s/test-site/htdocs/index.Php on line 13 Notice: Use of undefined constant main - assumed 'main' in /www/zxq.Net/t/e/s/test-site/htdocs/index.Php on line 16 


Its in the index.Php document I know but I don't know what to do ive coppied exactly the same I changed all the other settings of other things except this what must I change here ?

I'm a amateur sorry :P  

-reply by Taylan



Where is the rest of this tutorial?Please post it.I need it fast.By the way,the website link is broken.


Interesting start, but not quite up to spec yet. The PHP MySql is a powerful open source bit, this is to be sure. May I ask what is the major other ways of making a forum, I know there's other database programs, but is this the main way. Also I'd figure there are other programs designed to better handle forums.


ok well you guys are all nuts cause I got it to work...I'm waiting for the rest of the script.

 first I got Failed to open DB ok then I modified the thing and now it connects to database and no problems ok.Then I made a notepad document and thats what I use to make my pages and easily made it work!! I need the rest tho.

ps:I know nothing about mysql,javascript,ajax etc. I only know some css and I know some html.

and I'm no pc genious but I am very good at them and I'm having no problems. So u guys that are having problems you are nuts.

sorry for calling some of u nuts.

-reply by Dsialex(my ds site)