下面这个用PHP来制作评论系统教程由图老师小编精心推荐选出,过程简单易学超容易上手,喜欢就要赶紧get起来哦!
【 tulaoshi.com - Web开发 】
我们在实际做的过程中很是简单的,希望大家好好研究一下,给补充多点功能。本程序须在PHP and mySQL的环境下运行。有三个文件:comments.php, 是用来显示评论的, commentadd.php, 用来处理评论内容的, commentform.html 通过FROM来提交评论。
1.首先建立一个数据库,如果已经建立则建立一个符合条件的表:
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` ) );
评论查看页:COMMENTS.PHP,具体内容为(有用户名和密码的在实际工作中要改变):
$dbcnx = mysql_connect("localhost", "username", "password");mysql_select_db("comments");
接下来需要对表进行查询,并且把ID 按Descending: 顺序排序:
$result = mysql_query("SELECT * FROM comtbl ORDER BY postID DESC");if (!$result) {echo("bError 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"];
现在到了最关键的一步了,也是困难的一步: 因为在这里用到MySQL's TIMESTAMP 函数 (功能是可以自动的饿把时间添加到一个表中),并且需要取得时间的字符串,使用字符串函数substr() ( $yr 表示年, $mo 表示月, 等等):
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)$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);
还需要对上述代码的功能加以扩充来实现12或24小时表示或者用 AM和PM来表示上下午,代码如下:
if ($hr "11") {$x = "12";$timetype = "PM";$hr = $hr - 12;}else{$timetype = "AM";}
另外,当评论者要是留下Email 的话,我们可以在这里来建立一个连接实现联系发评论的人.代码如下:
if (!$url) {$url = "#";}else{$stat = $url;$url = "mailto:" . $url . "";}
最后,我们可以按行来显示数据,并且关闭循环,最终的显示代码如下:
echo("pb$msgTitle/b $msgTxtbr div align=right$hr:$min $timetype | $mo/$da/$yr | $msgId, a href='$url'$SigName/a/div/p");} pbMessage Title/b Text within the message, blah blahbr div align=rightHour:Minute AM/PM | Month/Day/Year | Message ID, a href='mailto:test@test.com'Name with email link/a/div/p
表单处理的程序: COMMENTADD.PHP
首先我们设置一些变量,然后通过表单把变量获得的数据提交到后台数据库中,并且请记住用户名和密码。
$assume = $_POST['assume']; $posterEMAIL = $_POST['postemail']; $postTXT = $_POST['posttxt']; $posterNAME = $_POST['poster']; $postTITLE = $_POST['posttitle'];if ($assume == "true") {$dbcnx = mysql_connect("localhost", "username", "password");mysql_select_db("comments");$sql = "INSERT INTO comtbl SET posterNAME='$posterNAME', posterEMAIL='$posterEMAIL',postTXT='$postTXT', postTITLE='$postTITLE'";if (mysql_query($sql)) {echo("PYour comment has been added/P");} else {echo("PError adding entry: " . mysql_error() . "/P");}}
提交了自己的评论之后还要有跳转的功能,下面的Javascript代码就可以实现跳转到指定的页面。
script language=javascript!--location.href="comments.php";//--/script
下面是具体的COMMENTFORM.HTML代码,通过下面的内容,可以让发表评论者发表评论,然后通过提交可以把数据提交到commentadd.php里面来实现数据的在线提交。
form action="commentadd.php" method=postinput 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"messagebr /input type=hidden name=assume value=trueinput type="submit" value="submit"
下面是处理评论的代码 comments.php:
?$dbcnx = mysql_connect("localhost", "username", "password");mysql_select_db("comments");$result = @mysql_query("SELECT * FROM comtbl ORDER BY postID DESC");if (!$result) { echo("bError 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("pb$msgTitle/b $msgTxtbrdiv align=right$hr:$min $timetype | $mo/$da/$yr | $msgId, a href='$url'$SigName/a/div/p");}?
下面是 commentadd.php:
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/webkaifa/)?$assume = $_POST['assume'];$posterEMAIL = $_POST['postemail'];$postTXT = $_POST['posttxt'];$posterNAME = $_POST['poster'];$postTITLE = $_POST['posttitle'];if ($assume == "true") {$dbcnx = mysql_connect("localhost", "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="comments.php";//--/script
整个程序到这就完了。
来源:http://www.tulaoshi.com/n/20160219/1611734.html