Forgot Password / Register
Site Statistics
Total Members: 520
Total Tutorials: 242
Newsest User: 8884244477
Todays Unique Hits: 673
0 Users 3 Guests Online

Comment System with MySQL

This tutorial will show you how to code a full comment system for your site. Well, lets get started, shall we?

Run this query through PHPMyAdmin

Code
CREATE TABLE `comtbl` (
`postID` INT NOT NULL AUTO_INCREMENT ,
`postTITLE` TEXT NOT NULL ,
`posterNAME` TEXT NOT NULL ,
`posterEMAIL` TEXT NOT NULL ,
`postTIME` TIMESTAMP NOT NULL ,
`postTXT` TEXT NOT NULL ,
PRIMARY KEY ( `postID` )
);


Start a new page and name it commentform.html and insert this code:

Code
<form action="commentadd.php" method=post> <input type="text" name="poster" size="23" value="name"><br /> <input type="text" name="posttitle" size="23" value="name"><br /> <input type="text" name="postemail" size="23" value="user@email.com"><br /> <textarea cols=44 rows=6 name="posttxt" size=24 wrap="VIRTUAL">message</textarea><br /> <input type=hidden name=assume value=true> <input type="submit" value="Add Comment">



index.php

PHP Code
1
2
<? $dbcnx mysql_connect("host""username""password"); //connects mysql_select_db("comments"); $result = @mysql_query("SELECT * FROM comtbl ORDER BY postID DESC"); if (!$result) {/grabs comments from db 
 
echo("<b>Error performing query: " mysql_error() . "</b>"); exit(); } while ($row mysql_fetch_array($result) ) { $msgTxt $row["postTXT"]; $msgId $row["postID"]; $SigName $row["posterNAME"]; $SigDate $row["postTIME"]; $msgTitle $row["postTITLE"]; $url $row["posterEMAIL"]; $yr substr($SigDate22); $mo substr($SigDate42); $da substr($SigDate62); $hr substr($SigDate82); $min substr($SigDate102); if ($hr "11") { $x "12"$timetype "PM"$hr $hr 12; }else{ $timetype "AM"; } if (!$url) { $url "#"; }else{ $stat $url$url "mailto:" $url ""; } echo("&ltp><b>$msgTitle</b> $msgTxt<br><div align=right> $hr:$min $timetype | $mo/$da/$yr | $msgId, <a href='$url'>$SigName</a></div></p>"); } ?>



commentadd.php

PHP Code
1
<? $assume $_POST['assume']; $posterEMAIL $_POST['postemail']; $postTXT $_POST['posttxt']; $posterNAME $_POST['poster']; $postTITLE $_POST['posttitle']; if ($assume == "true") { $dbcnx mysql_connect("host""username""password"); mysql_select_db("comments"); $sql "INSERT INTO comtbl SET posterNAME='$posterNAME', posterEMAIL='$posterEMAIL', postTXT='$postTXT', postTITLE='$postTITLE'"; if (mysql_query($sql)) { echo(" Your comment has been added "); } else { echo(" Error adding entry: " mysql_error() . " "); } } ?> <script language=javascript> <!-- location.href="index.php"; //--> </script>



Well, you're finished :D. I have a live preview here. Sorry it's not commented, it's hard to comment in this box I'm typing the tutorial in. lol.

NOTE: The live preview is edited to my likings with a CSS stylesheet added to make it look better. With the code above, your comment system will not turn out like mine without proper editing.
3xS
Author:
Views:
2619
Rating:
Posted on Sunday 8th March 2009 at 11:22 PM
ShadowMage
ShadowMage
Could..
Posted on Sunday 8th March 2009 at 03:41 PM
Adam981
Adam981
can this be used for a comment system for each page?
Posted on Sunday 4th May 2008 at 06:37 PM
JakkyD
JakkyD
Anyway to integrate this with the PHP User System so you have to login to comment?
Posted on Thursday 10th January 2008 at 11:17 PM
ShadowMage
ShadowMage
No need to post the whole thing in comments!
Posted on Wednesday 27th June 2007 at 07:49 PM
Dean
Dean
index.php
PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?
$dbcnx 
mysql_connect("host""username""password"); mysql_select_db("comments"); $result mysql_query("SELECT * FROM comtbl ORDER BY postID DESC");
if (!
$result) {
echo(
"<b>Error performing query: " mysql_error() . "</b>");
exit();
}
while (
$row mysql_fetch_array($result) )
{
$msgTxt $row["postTXT"];
$msgId $row["postID"]; $SigName $row["posterNAME"];
$SigDate $row["postTIME"]; $msgTitle $row["postTITLE"];
$url $row["posterEMAIL"];
$yr substr($SigDate22);
$mo substr($SigDate42);
$da substr($SigDate62);
$hr substr($SigDate82);
$min substr($SigDate102);
if (
$hr "11")
{
$x "12";
$timetype "PM";
$hr $hr 12;
}
else
{
$timetype "AM";
}
if (!
$url) { $url "#";
}else{
$stat $url;
$url "mailto:" $url "";
}
echo(
"
<b>
$msgTitle</b>
$msgTxt<br>
<div align=right>
$hr:$min $timetype | $mo/$da/$yr | $msgId,
<a href='
$url'>$SigName</a></div></p>");
}
?>


Sorry about previous comment forgot to close PHP Tag
Posted on Wednesday 27th June 2007 at 07:49 PM
Dean
Dean
index.php
[php]
<?
$dbcnx = mysql_connect("host", "username", "password"); mysql_select_db("comments"); $result = mysql_query("SELECT * FROM comtbl ORDER BY postID DESC");
if (!$result) {
echo("<b>Error performing query: " . mysql_error() . "</b>");
exit();
}
while ($row = mysql_fetch_array($result) )
{
$msgTxt = $row["postTXT"];
$msgId = $row["postID"]; $SigName = $row["posterNAME"];
$SigDate = $row["postTIME"]; $msgTitle = $row["postTITLE"];
$url = $row["posterEMAIL"];
$yr = substr($SigDate, 2, 2);
$mo = substr($SigDate, 4, 2);
$da = substr($SigDate, 6, 2);
$hr = substr($SigDate, 8, 2);
$min = substr($SigDate, 10, 2);
if ($hr > "11")
{
$x = "12";
$timetype = "PM";
$hr = $hr - 12;
}
else
{
$timetype = "AM";
}
if (!$url) { $url = "#";
}else{
$stat = $url;
$url = "mailto:" . $url . "";
}
echo("
<b>$msgTitle</b>
$msgTxt<br>
<div align=right>
$hr:$min $timetype | $mo/$da/$yr | $msgId,
<a href='$url'>$SigName</a></div></p>");
}
?>
Posted on Wednesday 27th June 2007 at 04:22 PM
warallthetm
warallthetm
Your index.php coding got messed up in the formatting