Use PHP to create a comment system

Source: Internet
Author: User
Please contact me with any Title: http://www.webjx.comweb @ webjx.com we in the actual process is very simple, hope you have a good study, to add more efficacy. This program must run in the PHPandmySQL environment. There are three files: What title please contact me: http://www.webjx.com web@webjx.com


We are very simple in the actual process. I hope you can study it 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 view 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 naming query: '. mysql_error ().'');
Exit ();
}

Because many records need to be read here, 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 ['poststername'];
$ SigDate = $ row ['posttime'];
$ MsgTitle = $ row ['posttitle'];
$ Url = $ row ['poststerm'];

Now the most important step is also a tough one: because MySQL's TIMESTAMP function is used here (the function is to actively add time to a table ), and the time string needs to be obtained. apply the string function substr () ($ yr represents year, $ mo represents 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 efficacy of the above code to achieve 12 or 24 hours of performance or use AM and PM to show the high and low 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
$ MsgTxt

$ Hr: $ min $ timetype | $ mo/$ da/$ yr | $ msgId, $ SigName

');

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.