Use PHP to make a comment system

Source: Internet
Author: User
If you have any questions please contact me: http://www.webjx.com web@webjx.com


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 handle comments, and 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. "";
}

Finally, we can display the data by row, and turn off the loop, and the final display code is as follows:

Echo ("<p><b>$msgTitle</b>
$msgtxt<br>
<div Align=right>$hr:$min $timetype | $mo/$da/$yr | $msgid, <a href= ' $url ' >$SigName</a></div></p> ');

}
<p><b>message title</b>
Text within the message, blah blah<br>
<div Align=right>hour:minute am/pm | Month/day/year | Message IDs, <a href= ' mailto:test@test.com ' >name with email link</a></div></p>

Procedure for form processing: Commentadd. Php

First we set up some variables and then submit the data from the form to the background database, and remember the username 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 ("<p>your comment has been added</p>");

} else {
Echo ("<p>error Adding Entry:".) Mysql_error (). "</P>");
}
}

After you have submitted your comments and have a jump function, the following JavaScript code can be implemented to jump to the specified page.

<script language=javascript>
<!--
Location.href= "comments.php";
-->
</script>

The following is the specific commentform.html code, which allows comments to be made by the author, and then submits the data online by submitting the data to the commentadd.php.

<form action= "commentadd.php" method=post>
<input type= "text" name= "poster" size= "value=" "Name" ><br/>
<input type= "text" name= "Posttitle" size= "value=" "Name" ><br/>
<input type= "text" name= "Postemail" size= "" value= "user@email.com" ><br/>
<textarea cols=44 rows=6 name= "posttxt" size=24 wrap= "VIRTUAL" >message<br
<input Type=hidden Name=assume value=true>
<input type= "Submit" value= "Submit" >

Here is the code comments.php for processing comments:
?
$DBCNX = mysql_connect ("localhost", "username", "password");
mysql_select_db ("comments");

$result = @mysql_query ("select * from Comtbl postid DESC");
if (!$result) {echo ("<b>error performing Query:".) Mysql_error (). "</b>");
Exit ();
}

while ($row = Mysql_fetch_array ($result)) {
$msgtxt = $row["Posttxt"];
$msgid = $row["PostID"];
$signame = $row["Postername"];
$sigdate = $row["Posttime"];
$msgtitle = $row["Posttitle"];
$url = $row["Posteremail"];
$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);

if ($hr > "11") {
$x = "12";
$timetype = "PM";
$HR = $hr-12;
}else{
$timetype = "AM";
}
if (!$url) {
$url = "#";
}else{
$stat = $url;
$url = "mailto:". $url. "";
}

Echo ("<p><b>$msgTitle</b> $msgtxt<br><div align=right>
$hr:$min $timetype | $mo/$da/$yr | $msgid, <a href= ' $url ' >$SigName</a></div></p> ');
}

?>
Here is commentadd.php:
?
$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 (). "");
}
}

?>
<script language=javascript>
<!--
Location.href= "comments.php";
-->
</script>

The whole program to this end, want to have their own comments on the system webmaster please try It

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.