Skip to Content
PHPCODE
Code Comment System with Ajax, PHP & MySQL
php code / September 8, 2018

Step by Step :-
Step 1: Created database and database name is comm
Step 2 : Created Table in SQL QUERY This

CREATE TABLE `comment` (
`id` int(11) NOT NULL,
`parent_id` int(11) NOT NULL,
`comment` varchar(200) NOT NULL,
`sender` varchar(40) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

ALTER TABLE `comment`
ADD PRIMARY KEY (`id`);

ALTER TABLE `comment`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
COMMIT;

Step 3 : Created header.php File

<!DOCTYPE html>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<link rel=”stylesheet” href=”https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css”>
<link rel=”stylesheet” href=”https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css”>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js”></script>
<script src=”https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js”></script>
<!– jQuery –>

Step 4 : Created footer.php File
<div class=”insert-post-ads1″ style=”margin-top:20px;”>
</div>
</div>
</body></html>
Step 5 :Created container.php File
</head>
<body class=””>
<div role=”navigation” class=”navbar navbar-default navbar-static-top”>
      <div class=”container”>
        <div class=”navbar-header”>
          <button data-target=”.navbar-collapse” data-toggle=”collapse” class=”navbar-toggle” type=”button”>
            <span class=”sr-only”>Toggle navigation</span>
            <span class=”icon-bar”></span>
            <span class=”icon-bar”></span>
            <span class=”icon-bar”></span>
          </button>
          <a href=”https://phpcodeinfomation.blogspot.com/p/php-script.html” class=”navbar-brand”>PHPCODE</a>
        </div>
        <div class=”navbar-collapse collapse”>
          <ul class=”nav navbar-nav”>
            <li class=”active”><a href=”https://phpcodeinfomation.blogspot.com/p/php-script.html”>Home</a></li>
          </ul>
        </div><!–/.nav-collapse –>
      </div>
    </div>
<div class=”container” style=”min-height:500px;”>
<div class=”>
</div>
Step 6 :Created index.php File
<?php
include(‘header.php’);
?>
<title>phpcode : Code Comment System with Ajax, PHP & MySQL</title>
<script src=”js/comments.js”></script>
<?php include(‘container.php’);?>
<div class=”container”>
<h2>Example: Comment System with Ajax, PHP & MySQL</h2>
<br>
<form method=”POST” id=”commentForm”>
<div class=”form-group”>
<input type=”text” name=”name” id=”name” class=”form-control” placeholder=”Enter Name” required />
</div>
<div class=”form-group”>
<textarea name=”comment” id=”comment” class=”form-control” placeholder=”Enter Comment” rows=”5″ required></textarea>
</div>
<span id=”message”></span>
<br>
<div class=”form-group”>
<input type=”hidden” name=”commentId” id=”commentId” value=”0″ />
<input type=”submit” name=”submit” id=”submit” class=”btn btn-primary” value=”Post Comment” />
</div>
</form>
<br>
<div id=”showComments”></div>
</div>
<?php include(‘footer.php’);?>
Step 7 : Created comments.php File
<?php
include_once(“db_connect.php”);
if(!empty($_POST[“name”]) && !empty($_POST[“comment”])){
$insertComments = “INSERT INTO comment (parent_id, comment, sender) VALUES (‘”.$_POST[“commentId”].”‘, ‘”.$_POST[“comment”].”‘, ‘”.$_POST[“name”].”‘)”;
mysqli_query($conn, $insertComments) or die(“database error: “. mysqli_error($conn));
$message = ‘<label class=”text-success”>Comment posted Successfully.</label>’;
$status = array(
‘error’  => 0,
‘message’ => $message
);
} else {
$message = ‘<label class=”text-danger”>Error: Comment not posted.</label>’;
$status = array(
‘error’  => 1,
‘message’ => $message
);
}
echo json_encode($status);
?>
Step 8 : Created db_connect.php File
<?php
/* Database connection start */
$servername = “localhost”;
$username = “root”;
$password = “”;
$dbname = “comm”;
$conn = mysqli_connect($servername, $username, $password, $dbname) or die(“Connection failed: ” . mysqli_connect_error());
if (mysqli_connect_errno()) {
    printf(“Connect failed: %sn”, mysqli_connect_error());
    exit();
}
?>
Step 9 : Created show_comments.php File
<?php
include_once(“db_connect.php”);
$commentQuery = “SELECT id, parent_id, comment, sender, date FROM comment WHERE parent_id = ‘0’ ORDER BY id DESC”;
$commentsResult = mysqli_query($conn, $commentQuery) or die(“database error:”. mysqli_error($conn));
$commentHTML = ”;
while($comment = mysqli_fetch_assoc($commentsResult)){
$commentHTML .= ‘
<div class=”panel panel-primary”>
<div class=”panel-heading”>By <b>’.$comment[“sender”].'</b> on <i>’.$comment[“date”].'</i></div>
<div class=”panel-body”>’.$comment[“comment”].'</div>
<div class=”panel-footer” align=”right”><button type=”button” class=”btn btn-primary reply” id=”‘.$comment[“id”].'”>Reply</button></div>
</div> ‘;
$commentHTML .= getCommentReply($conn, $comment[“id”]);
}
echo $commentHTML;
function getCommentReply($conn, $parentId = 0, $marginLeft = 0) {
$commentHTML = ”;
$commentQuery = “SELECT id, parent_id, comment, sender, date FROM comment WHERE parent_id = ‘”.$parentId.”‘”;
$commentsResult = mysqli_query($conn, $commentQuery);
$commentsCount = mysqli_num_rows($commentsResult);
if($parentId == 0) {
$marginLeft = 0;
} else {
$marginLeft = $marginLeft + 48;
}
if($commentsCount > 0) {
while($comment = mysqli_fetch_assoc($commentsResult)){
$commentHTML .= ‘
<div class=”panel panel-primary” style=”margin-left:’.$marginLeft.’px”>
<div class=”panel-heading”>By <b>’.$comment[“sender”].'</b> on <i>’.$comment[“date”].'</i></div>
<div class=”panel-body”>’.$comment[“comment”].'</div>
<div class=”panel-footer” align=”right”><button type=”button” class=”btn btn-primary reply” id=”‘.$comment[“id”].'”>Reply</button></div>
</div>
‘;
$commentHTML .= getCommentReply($conn, $comment[“id”], $marginLeft);
}
}
return $commentHTML;
}
?>
PHPCODE © 2024