06 March 2016

How to create comments system using PHP and MYSQL

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.

how,to,create,comments,system,make,comment,php,mysql,database,table,website,forum,blog,wordpress,programmer,lesson
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 :

how,to,create,comments,system,make,comment,php,mysql,database,table,website,forum,blog,wordpress,programmer,lesson
Simple System comments of the Facebook @wikihard

Google+ plus Example :

how,to,create,comments,system,make,comment,php,mysql,database,table,website,forum,blog,wordpress,programmer,lesson
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

      7 comments:

      1. In the body of the index, it seems that you have forgotten to close the while function and other than that it seems like mysql_connect Denies access, i will look into it more. As of right now nothing is working. Cheers!

        ReplyDelete
      2. Hi, thanks for your blog, if you want to learn about programming languages like java, php, android app, embedded system etc. I think this training institute is the best one.
        best python training in coimbatore
        Android training in coimbatore
        Networking training in coimbatore

        ReplyDelete
      3. with the production of promoter sites which builds their site traffic and in the end expands their deals. Top Expert Cakephp Developers

        ReplyDelete
      4. PHP is available at free of expense under PHP General Public License and most of its partnered required programming takes after MySQL, Text Editors and Apache Server are in like manner straightforwardly open, so it shows incredibly monetarily shrewd for the site owners.Why use Laravel

        ReplyDelete
      5. In particular, there are two difficulties that one experiences when managing software licenses. itools 4 crack license key

        ReplyDelete
      6. Hello, post gives very interesting information about off-season camping, really I like this information which is so much beneficial to us, keep sharing such kind of information, Thanks for sharing.
        iTools Crack

        ReplyDelete