Use PHP to create a comment system _ PHP Tutorial

Source: Internet
Author: User
Tags add time
Use PHP to create a comment system. We are very simple in the actual process. I hope you can study it and add more functions. This program must run in the PHPandmySQL environment. There are three files: comments. We are very simple in the actual process. I hope you can study them and add more functions. This program must run in the PHP and mySQL environment. There are three files: comments. php, which is used to display [commentadd. php, which is used to process the comment content. and commentform.html submits the comment through FROM.

1. First, create a database. if a database has been created, create a qualified table:
Create table 'comtbl '(
'Postid' int not null AUTO_INCREMENT,
'Posttitle' text not null,
'Poststername' text not null,
'Posteremail 'text not null,
'Posttime' timestamp not null,
'Posttxt 'text not null,
Primary key ('postid ')
);

Comment page: COMMENTS. PHP. the specific content is (the user name and password should 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 the order of Descending:

$ Result = mysql_query ("SELECT * FROM comtbl order by postID DESC ");
If (! $ Result ){
Echo (" Error configurming query: ". mysql_error ()."");
Exit ();
}

Here, because we need to read many records, we can read them cyclically. 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 the most important step is also a difficult step: because MySQL's TIMESTAMP function is used here (the function is to automatically add time to a table ), and the time string needs to be obtained. use the string function substr () ($ yr to represent the year, $ mo to represent 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 also need to expand the functions of the above code to achieve 12 or 24 hours, or use AM and PM to represent the previous afternoon. The code is as follows:

If ($ hr> "11 "){
$ X = "12 ";
$ Timetype = "PM ";
$ Hr = $ hr-12;
} Else {
$ Timetype = "AM ";
}

In addition, if the reviewer leaves an Email, we can establish a connection here to contact the reviewer. the code is as follows:

If (! $ Url ){
$ Url = "#";
} Else {
$ Stat = $ url;
$ Url = "mailto:". $ url ."";
}

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

Echo ("

$ MsgTitle


Bytes. This program must run in the PHP and mySQL environment. There are three files: comments...

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.