PHP Development Board php divider

User Options
Register--Login--Top 20 Posters--Search Topics


Forum Main>>Tutorials>>Simple forum in php/mysql

New Topic-Reply




Author
Post
Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged

PM ID and RPS ID: 1
PM [Chipmunk]

View Member Photo

Last replied to on Fri Feb 05, 2010 02:01:49
Edit Post|Quote
If you've ever wondered how to build a simple forum in PHP/Mysql, this simple tutorial will give you the basics of how to post and reply to threads
and display messages.

First we need to create the SQL tables which will store the data for our threads and posts for this forum:

Code:

CREATE TABLE forumtutorial_posts (
postid bigint(20) NOT NULL auto_increment,
author varchar(255) NOT NULL default '',
title varchar(255) NOT NULL default '',
post mediumtext NOT NULL,
showtime varchar(255) NOT NULL default '',
realtime bigint(20) NOT NULL default '0',
lastposter varchar(255) NOT NULL default '',
numreplies bigint(20) NOT NULL default '0',
parentid bigint(20) NOT NULL default '0',
lastrepliedto bigint(20) NOT NULL default '0',
PRIMARY KEY (postid)
)


This is pretty basic. It creates the fields for the id of the post(this is how each post is identified), the author, the title, the time of post, the number of replies, if it was a reply, the thread it is a reply of, the last person to reply, and when the thread was last replied to.

Next we have the basic connect.php file to connect to our database:

Code:

<?php

$db = mysql_connect("localhost", "username", "password") or die("Could not connect.");
if(!$db)
die("no db");
if(!mysql_select_db("database_name",$db))
die("No database selected.");
if(!get_magic_quotes_gpc())
{
$_GET = array_map('mysql_real_escape_string', $_GET);
$_POST = array_map('mysql_real_escape_string', $_POST);
$_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}
else
{
$_GET = array_map('stripslashes', $_GET);
$_POST = array_map('stripslashes', $_POST);
$_COOKIE = array_map('stripslashes', $_COOKIE);
$_GET = array_map('mysql_real_escape_string', $_GET);
$_POST = array_map('mysql_real_escape_string', $_POST);
$_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);
}

?>


Simple, a simple connector function where you have to input your username, password, and database name to connect. Not that the function below starting at
if(!get_magic_quotes_gpc()) is not actually part of the connector function, it is simply to parse out mySQL injections and may not be needed. If your php to mysql extenson classes are not updated, mysql_escape_string may not work and you will have to take the parser lines out.

Next we have a stylesheet to give our forum colors and look:

Code:

<!---

body {
a:link, a:visited, a:active { text-decoration: none}
font-family:Verdana, Sans-serif;
color; #000000;
font-size: 12px
}
input,textarea, select,{
color : #000000;
font: normal 12px;
border-collapse: collapse; border: 1px solid #000000;
}
.maintable {border: 0px ; width: 100%; padding: 0px; background-color: #FFFFFF} /*main table for forum*/
.regrow {font-family: Verdana,Sans-serif; color: #000000; font-weight: bold; background-color: #FFFFFF;font-size: 12px;} /*registration row, mainly here for symetry*/
.headline {font-family: Verdana,Sans-serif;font-weight: bold;color: #FFFFFF;background-color: #003366;font-size: 11px;} /*headline row, the first row that says forum name, topics, posts and such*/
.forumrow {font-family: Verdana,Sans-serif; color: #000000;background-color: #F2F2F2;font-size: 12px;} /*color of the forum rows*/
.mainrow a:link, a:visited, a:active { text-decoration: none;}
.mainrow {font-family: Verdana,Sans-serif; color: #000000;background-color: #F2F2F2;font-size: 12px; a:link, a:visited, a:active { text-decoration: none}} /*color of the forum rows*/
.maintables{background-color: #FFFFFF; width: 95%; padding: 0px; border: 1px solid; cellspacing: no} /*main table for forum*/
--->


Simple basic stylesheet to define some table headings and colors we will use, you can change this as you please.

Now the actual message board files. We only need 4 files, index.php(the one to display all the threads), post.php(The file to post and start threads),
message.php(The file that displays the messages), and reply.php(The file to reply to posts).

First index.php:

Code:

<?php
include "connect.php"; //mysql db connection here
print "<link rel='stylesheet' href='style.css' type='text/css'>";
print "<A href='post.php'>New Topic</a><br>";
print "<table class='maintable'>";
print "<tr class='headline'><td width=50%>Topic</td><td width=20%>Topic Starter</td><td>Replies</td><td>Last replied time</td></tr>";
$getthreads="SELECT * from forumtutorial_posts where parentid='0' order by lastrepliedto DESC";
$getthreads2=mysql_query($getthreads) or die("Could not get threads");
while($getthreads3=mysql_fetch_array($getthreads2))
{
$getthreads3[title]=strip_tags($getthreads3[title]);
$getthreads3[author]=strip_tags($getthreads3[author]);
print "<tr class='mainrow'><td><A href='message.php?id=$getthreads3[postid]'>$getthreads3[title]</a></td><td>$getthreads3[author]</td><td>$getthreads3[numreplies]</td><td>$getthreads3[showtime]<br>Last post by <b>$getthreads3[lastposter]</b></td></tr>";
}
print "</table>";

?>


This is straightforward, it simply selects all threads with a parentid of 0, meaning that all threads that are not replies and it selects them by the last time they were replied to, beginning with the latest first. Then it goes through a loop to display all of them along with the number of replies they have, and the thread starter. It links the thread subject with a message identifier to message.php so people can view the thread by clicking on the link.
The strip_tags function parses out SQL injections that user might have put in.

Now the message.php file:

Code:

<?php
include "connect.php"; //mysql db connection here
$id=$_GET['id'];
print "<link rel='stylesheet' href='style.css' type='text/css'>";
print "<A href='index.php'>Back to main forum</a>-<A href='post.php'>New Topic</a>-<A href='reply.php?id=$id'>Reply<br>";
print "<table class='maintable'>";
print "<tr class='headline'><td width=20%>Author</td><td width=80%>Post</td></tr>";
$gettopic="SELECT * from forumtutorial_posts where postid='$id'";
$gettopic2=mysql_query($gettopic) or die("Could not get topic");
$gettopic3=mysql_fetch_array($gettopic2);
print "<tr class='mainrow'><td valign='top'>$gettopic3[author]</td><td vakign='top'>Last replied to at $gettopic3[showtime]<br><hr>";
$message=strip_tags($gettopic3['post']);
$message=nl2br($message);
print "$message<hr><br>";
print "</td></tr>";
$getreplies="Select * from forumtutorial_posts where parentid='$id' order by postid desc"; //getting replies
$getreplies2=mysql_query($getreplies) or die("Could not get replies");
while($getreplies3=mysql_fetch_array($getreplies2))
{
print "<tr class='mainrow'><td valign='top'>$getreplies3[author]</td><td vakign='top'>Last replied to at $getreplies3[showtime]<br><hr>";
$message=strip_tags($getreplies3['post']);
$message=nl2br($message);
print "$message<hr><br>";
print "</td></tr>";
}
print "</table>";

?>


This file gets some data passed to it in the form $id in the URL.
What this code does is it first selects the topic post in $gettopic, printing it and then selects all the posts in $getreplies and prints them. Strip_tags is there to parse out HTML injectons and nl2br is there to make sure line breaks work right.

Now the post.php file

Code:

<?php
include "connect.php"; //connection string
print "<link rel='stylesheet' href='style.css' type='text/css'>";
print "<table class='maintables'>";
print "<tr class='headline'><td>Post a message</td></tr>";
print "<tr class='maintables'><td>";
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$yourpost=$_POST['yourpost'];
$subject=$_POST['subject'];
if(strlen($name)<1)
{
print "You did not type in a name."; //no name entered
}
else if(strlen($yourpost)<1)
{
print "You did not type in a post."; //no post entered
}
else if(strlen($subject)<1)
{
print "You did not enter a subject."; //no subject entered
}
else
{
$thedate=date("U"); //get unix timestamp
$displaytime=date("F j, Y, g:i a");
//we now strip HTML injections
$subject=strip_tags($subject);
$name=strip_tags($name);
$yourpost=strip_tags($yourpost);
$insertpost="INSERT INTO forumtutorial_posts(author,title,post,showtime,realtime,lastposter) values('$name','$subject','$yourpost','$displaytime','$thedate','$name')";
mysql_query($insertpost) or die("Could not insert post"); //insert post
print "Message posted, go back to <A href='index.php'>Forum</a>.";
}

}
else
{
print "<form action='post.php' method='post'>";
print "Your name:<br>";
print "<input type='text' name='name' size='20'><br>";
print "Subject:<br>";
print "<input type='text' name='subject' size='20'><br>";
print "Your message:<br>";
print "<textarea name='yourpost' rows='5' cols='40'></textarea><br>";
print "<input type='submit' name='submit' value='submit'></form>";

}
print "</td></tr></table>";
?>


This file has two cases, if submit was pressed, and if submit was not pressed. If submit was not pressed, then it prints a form for you to type in , your name, a message subject, and a message. If you have pressed submit, it checks first if the required fields of name, subject, and post have been entered
and spits out error message if the have not. If everything has been entered, then it draws the local time both in Unix format and in readable format and inserts all the data of the post along with the timestamps into the database.

Now the reply.php file:

Code:

<?php
include "connect.php"; //connection string
print "<link rel='stylesheet' href='style.css' type='text/css'>";
print "<table class='maintables'>";
print "<tr class='headline'><td>Reply</td></tr>";
print "<tr class='maintables'><td>";
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$yourpost=$_POST['yourpost'];
$subject=$_POST['subject'];
$id=$_POST['id'];
if(strlen($name)<1)
{
print "You did not type in a name."; //no name entered
}
else if(strlen($yourpost)<1)
{
print "You did not type in a post."; //no post entered
}
else
{
$thedate=date("U"); //get unix timestamp
$displaytime=date("F j, Y, g:i a");
//we now strip HTML injections
$subject=strip_tags($subject);
$name=strip_tags($name);
$yourpost=strip_tags($yourpost);
$insertpost="INSERT INTO forumtutorial_posts(author,title,post,showtime,realtime,lastposter,parentid) values('$name','$subject','$yourpost','$displaytime','$thedate','$name','$id')";
mysql_query($insertpost) or die("Could not insert post"); //insert post
$updatepost="Update forumtutorial_posts set numreplies=numreplies+'1', lastposter='$name',showtime='$displaytime', lastrepliedto='$thedate' where postid='$id'";
mysql_query($updatepost) or die("Could not update post");
print "Message posted, go back to <A href='message.php?id=$id'>Message</a>.";
}

}
else
{
$id=$_GET['id'];
print "<form action='reply.php' method='post'>";
print "<input type='hidden' name='id' value='$id'>";
print "Your name:<br>";
print "<input type='text' name='name' size='20'><br>";
print "Your message:<br>";
print "<textarea name='yourpost' rows='5' cols='40'></textarea><br>";
print "<input type='submit' name='submit' value='submit'></form>";

}
print "</td></tr></table>";
?>

Note this is very similar to the post.php file except it passed a hidden parameter id, which indicates which thread you are reply to. Also after you have pressed submit, it checks if the required fields have been filled out, inserts the message with the timestamps and then it updates the original message which you replied to with new timestamps and adds one to the number of replies that message has while putting you as the last replier to that message.

Ok, there you have it, a simple working forum.

-----------------------------
Chipmunk,
Supreme Administrator

Matt
Rank:acorn
Group: members
Posts: 1
IP Logged
PM ID and RPS ID: 11664
[PM Matt]

RPS score: 0
RPS challenge

Posted at Mon Jan 29, 2007 02:13:30
Edit post|Quote
Nice tut man!

PcPostar
Rank:acorn
Group: members
Posts: 2
IP Logged
PM ID and RPS ID: 11686
[PM PcPostar]

RPS score: 0
RPS challenge

Posted at Fri Feb 02, 2007 07:08:53
Edit post|Quote
Hi!

i've been looking for a long time to find such a great forum. Simple implementation in site, and the very free to costumize! Good work!

Ok, one question...
How can I correct the script so posts will inside one topic appear in a different order.
Now the old topics are at the bottom, and the new are on the top. How to do the oposite?
Help!

Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]

View Member Photo

Posted at Fri Feb 02, 2007 07:49:54
Edit post|Quote
instead of order by postid desc order by postid asc should do the trick.
-----------------------------
Chipmunk,
Supreme Administrator

PcPostar
Rank:acorn
Group: members
Posts: 2
IP Logged
PM ID and RPS ID: 11686
[PM PcPostar]

RPS score: 0
RPS challenge

Posted at Fri Feb 02, 2007 16:06:23
Edit post|Quote
Thanks!
(Quite new at databases, and didn't know the trick..)

Thanks

ithapiri
Rank:acorn
Group: members
Posts: 9
IP Logged
PM ID and RPS ID: 1380
[PM ithapiri]

Posted at Sat Feb 03, 2007 00:56:48
Edit post|Quote
just take some time & read chipmunk scripts ..all scripts are neatly written ..easly to understand ..tks admin

youbou
Rank:acorn
Group: members
Posts: 1
IP Logged
PM ID and RPS ID: 11732
[PM youbou]

RPS score: 0
RPS challenge

Posted at Mon Feb 12, 2007 07:35:18
Edit post|Quote
its very good and working perfectly but how do you make it so that when you create a topic it automatically starts at the top not bottom, i like the fact that when you post on a topic it goes to the top but how can you make it so that new topics start at the top thanks.

Snaket
Rank:acorn
Group: members
Posts: 1
IP Logged
PM ID and RPS ID: 11889
[PM Snaket]

RPS score: 0
RPS challenge

Posted at Fri Mar 09, 2007 02:24:19
Edit post|Quote
Alright this tutorial seemed easy, so I decided to check it out.

Now, does anyone have a sort of "advanced" method on how to include an actual forum view like the Chipmunk Forums? And anyone know how to make a register/log in page and how to have an admin contol panel with access to adding forums and allowing moderators?

Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]

View Member Photo

Posted at Fri Mar 09, 2007 04:31:42
Edit post|Quote
Yeah, but generally these tutorials are not going to be that long. I could write a short book about that. The entire forum consists of many, many files and thousands of lines of actual code.
-----------------------------
Chipmunk,
Supreme Administrator

Mindtrick
Rank:acorn
Group: members
Posts: 1
IP Logged
PM ID and RPS ID: 12037
[PM Mindtrick]

RPS score: 0
RPS challenge

Posted at Sun Mar 25, 2007 13:30:46
Edit post|Quote

I can't seem to get this working...I have a problem with connect.php... I get "No database selected." when all my information is correct anyone got some ideas??


~Mindtrick


-----------------------------
Hello! This is my signature!!

Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]

View Member Photo

Posted at Sun Mar 25, 2007 16:30:54
Edit post|Quote
That  means you don't have the database name correct because its not recognizing the name of your database.
-----------------------------
Chipmunk,
Supreme Administrator

Nunet
Rank:acorn
Group: members
Posts: 3
IP Logged
PM ID and RPS ID: 12201
[PM Nunet]

RPS score: 0
RPS challenge

Posted at Thu Apr 05, 2007 18:21:33
Edit post|Quote
Hi got forum problems

Nunet
Rank:acorn
Group: members
Posts: 3
IP Logged
PM ID and RPS ID: 12201
[PM Nunet]

RPS score: 0
RPS challenge

Posted at Thu Apr 05, 2007 18:30:33
Edit post|Quote

Hi,


Forum works well enough but still get error messages as follows:





Notice: Use of undefined constant title - assumed 'title' in /h***/****/1/c**/index.php on line 11Notice: Use of undefined constant title - assumed 'title' in /h***/****/1/c**//index.php on line 11Notice: Use of undefined constant author - assumed 'author' in /h***/****/1/c**/index.php on line 12Notice: Use of undefined constant author - assumed 'author' in /h***/****/1/c**/index.php on line 12


This is my line 11 and 12:


$getthreads3[title]=strip_tags($getthreads3[title]);


$getthreads3[author]=strip_tags($getthreads3[author]);


Also have this error message after I post a reply in reply.php:


Notice: Undefined index: subject in /h***/****/1/c**/reply.php on line 11


This is line 11:


 $subject=$_POST['subject'];


Not sure what is wrong as I have followed the forum to the letter.


Can you please help me???


 


 




Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]

View Member Photo

Posted at Thu Apr 05, 2007 19:07:41
Edit post|Quote
Turn debug mode off in PHP and those will go away.
-----------------------------
Chipmunk,
Supreme Administrator

Nunet
Rank:acorn
Group: members
Posts: 3
IP Logged
PM ID and RPS ID: 12201
[PM Nunet]

RPS score: 0
RPS challenge

Posted at Fri Apr 06, 2007 06:55:03
Edit post|Quote
Sorry, i'm fairly new at this but how do I do that if i'm working in PHPMYADMIN?

Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]

View Member Photo

Posted at Fri Apr 06, 2007 19:19:28
Edit post|Quote
You can't in PHPMYADMIN, it has to be done in php.ini . You may have to talk to your host about this one.
-----------------------------
Chipmunk,
Supreme Administrator

Growe
Rank:acorn
Group: members
Posts: 21
IP Logged
PM ID and RPS ID: 12448
[PM Growe]

RPS score: 0
RPS challenge

Posted at Sat Apr 21, 2007 14:05:40
Edit post|Quote
Quote:
  its very good and working perfectly but how do you make it so that when you create a topic it automatically starts at the top not bottom, i like the fact that when you post on a topic it goes to the top but how can you make it so that new topics start at the top thanks.
I would also like to know how to make new topics appear at the top of the screen. I have already tried changing a line from DESC to ASC but this fix the fault. Can anyone assist with this minor problem. Many thanks. G

Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]

View Member Photo

Posted at Sat Apr 21, 2007 14:40:16
Edit post|Quote

Quote:
 instead of order by postid desc order by postid asc should do the trick.


 


This worked for the first person that asked.


-----------------------------
Chipmunk,
Supreme Administrator

Growe
Rank:acorn
Group: members
Posts: 21
IP Logged
PM ID and RPS ID: 12448
[PM Growe]

RPS score: 0
RPS challenge

Posted at Sat Apr 21, 2007 14:52:34
Edit post|Quote
I tried that... and true is does put the new posts at the top of the screen with zero replys, however it then puts the posts that have the most recent replys at the bottom of the screen.Its something related to the lastrepliedto date and the script not recognising the date of a post with zero replys because it in reality doesn't have a lastrepliedto date because it hasn't been replied to yet.I hope I'm making some sort of sense so this problem can be fixed. Sorry if now. G

Chipmunk

Rank:Settler of Bobland
Group: Head Administrator
Posts: 2867
IP Logged
PM ID and RPS ID: 1
[PM Chipmunk]

View Member Photo

Posted at Sat Apr 21, 2007 16:01:26
Edit post|Quote
Do order by lastreplied desc then
-----------------------------
Chipmunk,
Supreme Administrator

Growe
Rank:acorn
Group: members
Posts: 21
IP Logged
PM ID and RPS ID: 12448
[PM Growe]

RPS score: 0
RPS challenge

Posted at Sat Apr 21, 2007 16:06:30
Edit post|Quote
It already is, however messages that are created appear at the bottom of the list of topics because they have no replys. Even though the date is newer than the one at the top.

Page: 1 2 3 4 5  >>   Last



Powered by © Chipmunk Board

Flash games Ninja games-Web Design New York