Use PHP to make a comment system

Source: Internet
Author: User
Tags add time comments php and php and mysql

We are doing in the actual process is very simple, I hope we have a good study, to add more functions. This procedure must be run in PHP and MySQL environment. There are three files: comments.php, which is used to display comments, commentadd.php, to process comments, commentform.html to submit comments through from.

1. Establish a database first, and establish a qualifying table if it is established:

  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 View page: comments.php, specific content (with user name and password in the actual work to change):

$dbcnx = mysql_connect("localhost", "username", "password");
mysql_select_db("comments");

Next you need to query the table and sort the IDs by descending:

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

Here because to read a lot of records, so use the loop to read, the specific procedures are 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 it's the most critical step, is also a difficult step: because the MySQL ' s TIMESTAMP function is used here (the function can be automatically hungry to add time to a table), and need to get the time of the string, using the String function substr () ($yr for the year, $mo for the month, And so on):

$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);

You will also need to extend the functionality of the above code to implement 12 or 24 hour representations or to use AM and PM to represent the previous PM with the following code:

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

In addition, when the reviewer leaves an email, we can set up a person to connect to the implementation link to comment. The code is as follows:

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.