Use PHP to create a comment system

Source: Internet
Author: User
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.php, which are used to display comment. commentadd.php is used to handle comment content. andcommentform.html uses FROM to submit comments. 1. first create a data


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 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 critical 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
$ MsgTxt

  

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

");

}
  

Message Title
Text within the message, blah

  

Hour: Minute AM/PM | Month/Day/Year | Message ID, Name with email link



Form processing program: COMMENTADD. PHP

First, we set some variables, and then submit the data obtained from the variables to the background database through the form. remember the user name and password.

$ 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 ()."

");
}
}

After submitting your comments, you must also have the jump function. the following Javascript code can jump to the specified page.

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.