Hello Programmers this is my first lesson in PHP and it talk about how to make and create system comments to your website or any websites you want. All
forums use this system and the most of websites.
![]() |
create comment system by using PHP and Mysql @wikihard |
I wish you take advantage of the lesson and learn more about PHP programming.
Notice : All big websites use COMMENTS SYSTEM like Facebook, Twiter and google plus.
Facebook Example :
![]() | |
Simple System comments of the Facebook @wikihard |
Google+ plus Example :
![]() |
Google plus comment @wikihard |
Google and Facebook are using Javascript, but in this topic I will show you how to create a simple system commenst by using PHP & MYSQL only, I need you to learn the basic, then you can improve it yourself!
firstly you must to create a php file called connect.php and put the script bellow in, This file will help us to manipulate between the website and database without any problem.
Connect.php File :
<?php
$user='username';
$user='username';
$pass='password'; //@wikihard copywrite
$host='localhost'; // if you have a web hosting change this infos
$database='public';
mysql_connect("$host", "$user", "$pass")or die("cannot connect");
mysql_select_db("$database")or die("cannot select DB");
?>
Read more : 3 ways to learn PHP on your own
Read more : 9 tips to become better in programming
When you finish connect.php file, we will start to create new important file called index.php, we will use this page to display our comments and to add more comments too.
<?php
require_once('connect.php');
$message ='';
if(isset($_POST['submit']))
{
if(!empty($_POST['comment']) && !empty($_POST['name']))
{
//created by @wikihard
$comment= $_POST['comment'];
$name= $_POST['name'];
$query = "INSERT INTO `comment` VALUES(NULL,'$name','$comment')";
$qresult = mysql_query($query);
$message="Thank you for comment!";
} else
{
$message="please don't let area empty!";
}
}
$sql = "SELECT * FROM `comment`";
$qsql = mysql_query($sql);
?>
<!DOCTYPE html>
<body>
<?php
while ($row = mysql_fetch_array($qsql))
{
echo "<b>".$row['name']."</b> : </br>";
echo " ".$row['comment'].". </br></br>";
?>
<form method="post" action="index.php">
<input type="text" name="name" placeholder="put your name"></br></br>
<textarea rows="5" cols="25" name="comment" placeholder="write your comment here"></textarea></br></br>
<input type="submit" name="submit" value="post comment">
</form>
<?php echo $message;?>
</body>
</html>
How it work :
- First we must call the connect.php file to connect with the database!
- When we add value and press the button "Add comment", the comment will send and save in the comments table(id,name,comment) in the database.
- if the fields are empty, will NOT WORK and this message will show for the user "please don't let area empty " Until he put values before submitting.
- Now we need to display all comments from the database (comments table) to the home page ( index.php ).
Any question please let a comment! Good luck