php mysql comment system --CodeNair.com - Web Development and Web Design Codes

Latest

Wednesday, February 7, 2018

php mysql comment system --CodeNair.com

php mysql comment system

php mysql comment system
php mysql comment system

Hi Guys In this lesson we will learn how to create a nice comments system with php MySQL. Comment's system is very important for any website..
follow the below to learn how to create php mysql comments system..
=>Database and Table:
    Datbase Name: codenair.com
   Table Name: topic and comments
Now Use Below SQL Codes to Create table topic
php mysql comment system
php mysql comment system

CREATE TABLE IF NOT EXISTS `topic` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(150) NOT NULL,
  `content` longtext NOT NULL,
  `author` varchar(50) NOT NULL,
  `total_comments` varchar(50) NOT NULL DEFAULT '0',
  PRIMARY KEY(id)
) ENGINE=MyISAM AUTO_INCREMENT=45 DEFAULT CHARSET=latin1;
and Insert some rows in topic table using below SQL Codes:

INSERT INTO `topic` (`id`, `title`, `content`, `author`, `total_comments`) VALUES
(NULL, 'Install Windows 7', 'How to install Windows 7','Suny', 0),
(NULL, 'Install Windows 7', 'How to Install Windows 7','Jack', 0),
(NULL, 'How to Live Tv', 'This is how to live TV','Hrithik', 0),
(NULL, 'ANJAN', 'This is Test Content', 'ANJAN',0);

 Now Use Below SQL Codes to Create comments Table:
php mysql comment system
php mysql comment system

CREATE TABLE IF NOT EXISTS `comments` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `content_id` int(20) NOT NULL,
  `name` varchar(50) NOT NULL,
  `description` longtext NOT NULL,
  primary key(id)
) ENGINE=MyISAM AUTO_INCREMENT=26 DEFAULT CHARSET=latin1;

Now Create 5 files named:
1. connect.php
2. index.php
3. topic.php
4. comment.php
5. style.css
Now Copy the below codes for each file

1. connect.php

<?php
$conn=new mysqli('localhost','root','','codenair.com');
?>

2. index.php 
index page will show whole topic from database with total numbers of comments and single topic page link.like below..
php mysql comment system
php mysql comment system


<html>
<head>
<title>PHP MySQL Comments System</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<div id="wrapper">
<h2>PHP MySQL Comments System</h2>
<?php
include("connect.php");
$result=mysqli_query($conn,"SELECT * FROM topic LIMIT 5");
while($row=$result->fetch_array()){?>
     <div id="title"><a href="topic.php?id=<?php echo $row['id'];?>"><?php echo $row['title'];?></a></div>
     <p><?php echo $row['content'];?></p>
<div id="info">Author: <?php echo $row['author']?> || Comments: <?php echo $row['total_comments'];?></div>
  <?php } ?>
</div>
</html>

3. topic.php
topic page will show single topic with comments and comments box...like below
php mysql comment system
php mysql comment system
<html>
<head>
<title>PHP MySQL Comments System</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<div id="wrapper">
<a href="index.php"><h2>PHP MySQL Comments System</h2></a>
<?php
include("connect.php");
$id=$_GET['id'];
//Selecting Topic from Topic table by ID
$result=mysqli_query($conn,"SELECT * FROM `topic` WHERE id='$id'");
$row=$result->fetch_assoc();
    ?>
     <div id="title"><?php echo $row['title'];?></div>
     <p><?php echo $row['content'];?></p>
<div id="info">Author: <?php echo $row['author']?> || Comments: <?php echo $row['total_comments'];?></div>
 
<div id="comment">
<div id="title">Comments:</div>
    <?php
 //Selecting Comment from comments Table by Topic ID
   $comment=mysqli_query($conn,"SELECT*FROM comments WHERE content_id='$id' ORDER BY ID DESC LIMIT 5");
   $count=$comment->num_rows;
   if($count>0){
?>
    <?php  while($comments=$comment->fetch_array()){?> 
<div id="info"><?php echo $comments['name'];?></div>
<p><?php echo $comments['description'];?></p>
   <?php }} else {?>
  <div id="title">No Comments Yet</div>
<?php };?>
</div>
<!--Comment box-->
   <div id="commentbox">
     <form method="POST" action="comment.php">
     <input type="hidden" name="content_id" value="<?php echo $id;?>"/>
     <table>
       <tr>
         <td><span>Your Name:</span></td>
         <td><input type="text" name="name" required/></td>
       </tr>
       <tr>
         <td><span>Comment:</span></td>
         <td><textarea name="description" rows="10" cols="30" required/></textarea></td>
       </tr>
       <tr>
       <td></td>
       <td><input type="submit" name="submit" value="Comment Now"/></td>
       </tr>
     </table>
      
     </form>
   </div>
</div>
</html>
4. comment.php
comment.php file to store comments and other data to database..


<?php
include("connect.php");
if(isset($_POST['submit'])){
    $id=mysqli_real_escape_string($conn,$_POST['content_id']);
    $name=mysqli_real_escape_string($conn,$_POST['name']);
    $comment=mysqli_real_escape_string($conn,$_POST['description']);
  //Inserting Comment
   $result=mysqli_query($conn,"INSERT INTO comments(content_id,name,description)VALUES('$id','$name','$comment')");
   //Counting Total Comments By content_id
   $total=mysqli_query($conn,"SELECT*FROM comments WHERE content_id='$id'");
   $countcomment=$total->num_rows;
   //Updating Our Topic table-> 'total_comments' field
   $totalcomment=mysqli_query($conn,"UPDATE topic SET total_comments='$countcomment' WHERE id='$id'");
   if($totalcomment){
      header("location:topic.php?id=".$id);
   }else{
    echo 'Sorry Something went wrong';
   }   
}
?>
5. style.css
style.css file to design our index and topic page

#wrapper{
    background:#ddd;
    margin:auto;
    width:800px;
}
#comment{
    background:#ccc;
    width:600px;
    float:right;
}
#commentbox{
    float:right;
}
span{
    font-size:18px;
    font-weight:bold;
    font-style:italic;
    color:green;
}
input[type=text]{
    width:250px;
    border-radius:5px;
}
input[type=submit]{
    padding:8px;
    background:black;
    color:white;
    border-radius:5px;
 cursor:pointer;
}
textarea{
    width:250px;
    border-radius:5px;
}
h2{
    color:red;
    text-align:center;
}
a{
    color:white;
    text-decoration:none;
}
a:hover{
    color:blue;
}
#title{
    background:black;
    color:red;
    font-size:22px;
     
}
#info{
    font-size:18px;
    text-transform:capitalize;
    background:green;
    color:white;
    font-style:italic;
    margin-bottom:2px;
}
#comment p{
 margin-left:50px;
 margin:5px 0 0 20px;
 
}
That's it guys how to create php mysql comments system..keep visiting for more codes and don't forget to leave your comment..Thank You...

1 comment:

Thank You for Your Comment

Note: Only a member of this blog may post a comment.