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
}
Message Title
Text within the message, blah blah
表单处理的程序: 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里面来实现数据的在线提交。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

In PHPOOP, self:: refers to the current class, parent:: refers to the parent class, static:: is used for late static binding. 1.self:: is used for static method and constant calls, but does not support late static binding. 2.parent:: is used for subclasses to call parent class methods, and private methods cannot be accessed. 3.static:: supports late static binding, suitable for inheritance and polymorphism, but may affect the readability of the code.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.
