Home Know Me Snaps Feedback
 
::Online Microsoft Office,Web ,Graphic and many more Tutorials
A simple comment system using PHP and MySQL
Firstly, the contents of the file db_connect.php. What I’ve done here is simply place the mysql_connect() function within the file db_connect.php to save us having to type out the database connection code twice, as we’ll be adding that into our initial HTML file, mysql_form.php, in just a moment. Here is the connection file as it is set up on my computer (you may need to change the values to suit your installation of PHP)

<?php
$link = mysql_connect('localhost', 'root', 'pass'); //Connects to the database at "localhost"
mysql_select_db ('test', $link); //Assuming you have a database named "test" set up
?>

Secondly, you’ll need the structure for the “comments” table. This should be created in the same database as is defined in the connection file above
CREATE TABLE IF NOT EXISTS `comments` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(64) NOT NULL,
`comment` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

First, here’s the code you’ll need to use to generate the HTML form. Save this file somewhere in your PHP directory, with the name mysql_form.php

A simple PHP/MySQL form
<h1>Please enter your name and a comment</h1>
<form action="form_submit.php" method="post">
Name : <input size="32" type="text" name="username" /><br>
Comment : <textarea cols="40" rows="5" name="comment"></textarea><br>
<input type="submit" value="Send comment" /> </form>

Next, we need to create another file which will receive the request from the user, and process the data accordingly. We have specified that this file will be called form_submit.php. Create this file, and within it add the following code

Sumbit Comment Without Checking for invalid data

<?php
include("db_connect.php");
$username = $_POST['username'];
$comment = $_POST['comment'];
if(get_magic_quotes_gpc()){
$username = stripslashes($username);
$comment = stripslashes($comment);
}
$username = mysql_real_escape_string($username);
$comment = mysql_real_escape_string($comment);
 
$result = mysql_query("INSERT INTO comments (name, comment) VALUES ('$username', '$comment')");
if($result == true) {
echo "The comment was added successfully";
} else {
echo "The comment could not be added, there was an error";
}
?>
<br/><a href="mysql_form.php">Go back to the comments page</a>

Sumbit Comment With Checking for invalid data

<?php
include("db_connect.php");
$username = $_POST['username'];
$comment = $_POST['comment'];
if(get_magic_quotes_gpc()){
$username = stripslashes($username);
$comment = stripslashes($comment);
}
$username = mysql_real_escape_string($username);
$comment = mysql_real_escape_string($comment);
if(strlen($username) == 0 || strlen($comment) == 0){
echo "You didn't enter all the data. Please go back and retry";
} else {
$result = mysql_query("INSERT INTO comments (name, comment) VALUES ('$username', '$comment')");
if($result == true) {
echo "The comment was added successfully";
} else {
echo "The comment could not be added, there was an error";
}
}
?>
<br/><a href="mysql_form.php">Go back to the comments page</a>

Modifying the HTML file to display the user comments

With a little bit of modification, we can adjust the file we created earlier, mysql_form.php to display all the comments submitted so far

<?php
include("db_connect.php");
$comments = mysql_query("SELECT * FROM comments ORDER BY id DESC LIMIT 0, 10");
?>
A simple PHP/MySQL form
<h2>Please enter your name and a comment</h2>
<form action="form_submit.php" method="post">
Name :
<input size="32" type="text" name="username" /><br>
Comment : <textarea cols="40" rows="5" name="comment"></textarea><br>
<input type="submit" value="Send comment" />
</form>
<h2>Here are the comments submitted so far</h2>
 
<?php
if($row = mysql_fetch_array($comments)){
do {
echo "<h3>{$row['name']}</h3>";
echo "{$row['comment']}";
} while ($row = mysql_fetch_array($comments));
} else {
echo "There are no comments";
}
 
?>

In our extended example, we’re making use of the ORDER BY and LIMIT keywords to show only the 10 latest comments, in order from newest to oldest.

If you try out the code above, you should see the same form on the page mysql_form.php, and underneath, a list of all the comments you’ve added. If you try adding a new one, and then return to this page, you should see it appear at the top of the list

  PHP Table of Content
  Php sessions System
  Create a PHP contact form
  Comment System with php/mysql
  Admin Login and Delete Comment
  Simple file upload
  File Upload Function
  Connect with mysql database
 
   
 
 
       
   
© 2008, krishnakumar.com.np. All rights reserved
.