Система комментирования с помощью ajax php mysql
Не понимаю почему комментарии не добавляются в БД, при заполнении формы выводиться только надпись "Comment posted Successfully.". Помогите пожалуйста, уже руки опускаются 1)db.connect.php
$host = 'localhost';
$user = 'root';
$password = 'root';
$dbname = 'phpzag';
$dsn = "mysql:host=$host;dbname=$dbname;charset=utf8";
$pdo = new PDO($dsn,$user,$password);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
2)Таблица mysql
CREATE TABLE `comment` (
`id` int NOT NULL AUTO_INCREMENT,
`parent_id` int NOT NULL,
`comment` varchar(200) NOT NULL,
`sender` varchar(40) NOT NULL,
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
3)index.php
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Главная страница</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="comments.js"></script>
</head>
<body>
<div class="container">
<h2>Example: Comment System with Ajax, PHP & MySQL</h2>
<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>
<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>
<div id="showComments"></div>
</div>
</body>
</html>
4)comments.js
$(document).ready(function () {
$('#commentForm').on('submit',function (event) {
event.preventDefault();
var formData = $(this).serialize();
$.ajax({
url: "comments.php",
method: "POST",
data: formData,
dataType: "JSON",
success : function (response){
if (!response.error){
$('#commentForm')[0].reset();
$('#commentId').val('0');
$('#message').html(response.message);
showComments();
}
else if (response.error){
$('#message').html(response.message);
}
}
})
});
});
function showComments(){
$.ajax({
url: 'showComments.php',
method: 'POST',
success: function (response){
$('#showComments').html(response);
}
})
}
5)comments.php
include_once 'db_connect.php';
if (!empty($_POST['name']) && !empty($_POST['comment'])) {
$commentId = $_POST['commentId'];
$name = $_POST['name'];
$comment = $_POST['comment'];
$sqlInsert = "INSERT INTO `comment`(`parent_id`, `comment`, `sender`) VALUES (':commentId',':comment',':name');";
$stmtInsert = $pdo->prepare($sqlInsert);
$stmtInsert->execute([':commentId' => $commentId, ':comment' => $comment, ':name' => $name]);
$message = '<label class="text-success">Comment posted Successfully.</label>';
$status = ['error' => 0, 'message' => $message];
}
else{
$message = '<label class="text-danger">Error: Comment not posted.</label>';
$status = ['error'=>1,'message'=>$message];
}
echo json_encode($status);
6)showComments.php
<?php
include_once 'db_connect.php';
$sqlSelect = "SELECT * FROM `comment` WHERE parent_id = ':parent_id' ORDER BY id DESC;";
$stmtSelect = $pdo->prepare($sqlSelect);
$stmtSelect->execute(['parent_id'=>0]);
$comments = $stmtSelect->fetchAll(PDO::FETCH_ASSOC);
$commentHTML = '';
foreach ($comments as $comment){
$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($pdo,$comment['id']);
}
echo $commentHTML;
function getCommentReply($pdo,$parentId = 0, $marginLeft = 0){
$commentHTML = '';
$sqlCount = "SELECT COUNT(id) FROM comment;";
$stmtCount = $pdo->query($sqlCount);
$commentsCount = $stmtCount->fetchColumn();
if ($parentId==0){
$marginLeft = 0;
}
else{
$marginLeft+=48;
}
if ($commentsCount>0){
$sqlFetch = "SELECT * FROM comment WHERE parent_id = ':parentId';";
$stmtFetch = $pdo->prepare($sqlFetch);
$stmtFetch->execute([':parentId'=>$parentId]);
$comments = $stmtFetch->fetchAll(PDO::FETCH_ASSOC);
foreach ($comments as $comment){
$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($pdo,$comment['id'],$marginLeft);
}
}
return $commentHTML;
}