|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
用C语言重新编写,包括可以访问数据库。他将这些程序和一些表单直译器整合起来,称为 PHP/FI。PHP/FI 可以和数据库连接,产生简单的动态网页程序。 有甚么成绩请与我接洽:http://www.webjx.com web@webjx.com
咱们在实践做的过程当中很是复杂的,但愿人人好好研讨一下,给增补多点功效。本法式须在PHP and mySQL的情况下运转。有三个文件:comments.php, 是用来显示[评论的, commentadd.php, 用来处置评论内容的, and 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("<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"];
如今到了最关头的一步了,也是坚苦的一步: 由于在这里用到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("<p><b>$msgTitle</b>
$msgTxt<br>
<div align=right>$hr:$min $timetype | $mo/$da/$yr | $msgId, <a href='$url'>$SigName</a></div></p>");
}
<p><b>Message Title</b>
Text within the message, blah blah<br>
<div align=right>Hour: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("<P>Your comment has been added</P>");
} else {
echo("<P>Error adding entry: " . mysql_error() . "</P>");
}
}
提交了本人的评论以后还要有跳转的功效,上面的Javascript代码就能够完成跳转到指定的页面。
<script language=javascript>
<!--
location.href="comments.php";
//-->
</script>
上面是详细的COMMENTFORM.HTML代码,经由过程上面的内容,可让宣布评论者宣布评论,然后经由过程提交可以把数据提交到commentadd.php外面来完成数据的在线提交。
<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<br />
<input type=hidden name=assume value=true>
<input 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("<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("<p><b>$msgTitle</b> $msgTxt<br><div align=right>
$hr:$min $timetype | $mo/$da/$yr | $msgId, <a href='$url'>$SigName</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("Your comment has been added");
} else {
echo("Error adding entry: " . mysql_error() . "");
}
}
?>
<script language=javascript>
<!--
location.href="comments.php";
//-->
</script>
全部法式到这就完了,想具有本人评论体系的站长请尝尝吧会PHP吗?会,我会把一加一的运算写在函数里,然后调用啦! |
|