您好我已经创建了一个评论页面。当我按提交评论时,它会创建2条记录而不是1条而我无法理解它为何会这样做。
任何人都可以在我的代码中看到任何可能使其重复插入
的内容<!-- Blog Comments -->
<?php
if(isset($_POST['create_comment'])) {
$the_post_id = $_GET['p_id'];
$comment_author = $_POST['comment_author'];
$comment_email = $_POST['comment_email'];
$comment_content = $_POST['comment_content'];
if (!empty($comment_author) && !empty($comment_email) && !empty($comment_content)) {
$the_post_id = $_GET['p_id'];
$comment_author = $_POST['comment_author'];
$comment_email = $_POST['comment_email'];
$comment_content = $_POST['comment_content'];
$query = "INSERT INTO comments(comment_post_id, comment_author,
comment_email, comment_content,comment_status, comment_date)
VALUES($the_post_id, '{$comment_author}', '{$comment_email}',
'{$comment_content}','Unapproved' , now() )";
$create_comment_query= mysqli_query($connection,$query);
if(!$create_comment_query) {
die('QUERY FAILED'. mysqli_error($connection));
}
/* $query = "UPDATE posts SET post_comment_count = post_comment_count + 1 ";
$query .= "WHERE post_id = $the_post_id "; */
$update_comment_count = mysqli_query($connection,$query);
} else {
echo "<script>alert('Fields cannot be empty')</script>";
}
}
?>
<!-- Comments Form -->
<div class="well">
<h4>Leave a Comment:</h4>
<form role="form" action="" method="post">
<div class="form-group">
<label for="Author">Author :</label>
<input type="text" class="form_control" name="comment_author">
</div>
<div class="form-group">
<label for="Email">Email :</label>
<input type="email" class="form_control" name="comment_email">
</div>
<div class="form-group">
<label for="Comment">Your Comment :</label>
<textarea class="form-control" rows="3" name="comment_content"></textarea>
</div>
<button type="submit" class="btn btn-primary" name="create_comment">Submit</button>
</form>
</div>
<hr>
答案 0 :(得分:3)
在if if语句中,您有以下内容:
$create_comment_query = mysqli_query($conenction, $query);
然后你做:
$update_comment_count = mysqli_query($connection, $query);
我看到你评论了该查询应该更新的部分,但我猜这就是你遇到麻烦的原因。
因此,如果您删除应解决问题的$update_commment_count
。如果这有帮助,请告诉我!