您好,欢迎访问一九零五行业门户网

PHP来编写网站评论系统

首先打开dreamweaver cc,安装mysql数据库。
这里有三个文件:comments.php, 是用来显示评论的, commentadd.php, 用来处理评论内容的, commentform.html 通过from来提交评论。
首先建立一个数据库,如果已经建立则建立一个符合条件的表:
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(error performing query: . mysql_error() . );
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 表示月, 等等):
$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($msgtitle
$msgtxt
$hr:$min $timetype | $mo/$da/$yr | $msgid, $signame
);
}
message title
text within the message, blah blah
hour:minute am/pm | month/day/year | message id, name with email link
表单处理的程序: 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(your comment has been added
);
} else {
echo(error adding entry: . mysql_error() .
);
}
} 提交了自己的评论之后还要有跳转的功能,下面的javascript代码就可以实现跳转到指定的页面。
下面是具体的commentform.html代码,通过下面的内容,可以让发表评论者发表评论,然后通过提交可以把数据提交到commentadd.php里面来实现数据的在线提交。
message
下面是处理评论的代码 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(error performing query: . mysql_error() . );
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($msgtitle $msgtxt
$hr:$min $timetype | $mo/$da/$yr | $msgid, $signame
);
}
?> 下面是 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(your comment has been added);
} else {
echo(error adding entry: . mysql_error() . );
}
}
?>

php程序编写完毕,可以上传啦。
其它类似信息

推荐信息