search
HomeBackend DevelopmentPHP TutorialPHP来编写网站评论系统

  首先打开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里面来实现数据的在线提交。








Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment