Home  >  Article  >  Backend Development  >  Use PHP to create a comment system_PHP tutorial

Use PHP to create a comment system_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:00:071367browse

The actual process we make is very simple. I hope everyone will study it carefully and add more functions. This program must be run in the environment of PHP and mySQL. There are three files: comments.php, used to display comments, commentadd.php, used to process comment content, and commentform.html to submit comments through FROM.

1. First create a database. If it has already been created, create a table that meets the conditions:
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`)
);

Comment viewing page: COMMENTS.PHP, the specific content is (the username and password must be changed in actual work):
$dbcnx = mysql_connect("localhost", "username", "password");
mysql_select_db("comments");
Next, you need to query the table and sort the IDs in Descending: order:

​$result = mysql_query("SELECT * FROM comtbl ORDER BY postID DESC");
if (!$result) {
echo("Error performing query: " . mysql_error() . "");
exit();
}

Because there are many records to be read here, a loop is used to read them. The specific procedure is as follows:
while ($row = mysql_fetch_array($result) ) {
$msgTxt = $row["postTXT"];
$msgId = $row["postID"];
$SigName = $row["posterNAME"];
$SigDate = $row["postTIME"];
$msgTitle = $row["postTITLE"];
$url = $row["posterEMAIL"];

Now comes the most critical step, and it is also a difficult one: because MySQL's TIMESTAMP function is used here (the function is to automatically add time to a table), and you need to get the string of the time, use the string function substr () ($yr represents year, $mo represents month, etc.):

$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);
It is also necessary to expand the function of the above code to realize 12 or 24 hours expression or use AM and PM to express morning and afternoon. The code is as follows:

 if ($hr > "11") {
$x = "12";
$timetype = "PM";
$hr = $hr - 12;
}else{
$timetype = "AM";
}

In addition, if the commenter leaves an email, we can establish a connection here to contact the person who posted the comment. The code is as follows:

 if (!$url) {
$url = "#";
}else{
$stat = $url;
$url = "mailto:" . $url . "";
}

Finally, we can display the data by row and close the loop. The final display code is as follows:

echo("

$msgTitle


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445538.htmlTechArticleThe actual process we do is very simple. I hope everyone will study it carefully and add more functions. This program must be run in the environment of PHP and mySQL. There are three files: comments...
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