我的评论系统存在问题。目前只是简单地添加到数据库排序并在博客上的正确帖子下显示。我正在创建,我将在稍后实现ajax等。
我现在遇到的问题是它在我添加评论的帖子下面显示了帖子的最后评论。所以我要说我在10号帖子上添加评论,它会在10和11后显示评论。它排序正确,但它也重复到下面的帖子。 (它不会像数据库显示的那样在数据库中重复。)
<?php
// Connect to the database
include('../acp/db/db.php');
$link = mysql_connect($dbhost, $dbuser, $dbpassword, $dbname);
mysql_select_db($dbname);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query('SELECT * FROM `posts` ORDER BY id DESC') or die(mysql_error());
while($row = mysql_fetch_array($result)) {
$id_post = $row['id'];
echo " <!-- Blog Post Content Column -->
<h1> " . $row['post_title'] . " </h1><p class='lead'>
by <a href='#'>Matt</a></p> <hr>
<p><span class='glyphicon glyphicon-time'>" . $row['date_created'] . "</span></p>
<img class='img-responsive' style='width: 900px;height: 300px;' src=" . $row['post_img'] . "> <hr>
<p class='lead'>" . $row['post_first'] . "</p>
<p>" . $row['post_second'] . "</p> <hr>
<!-- Comments Form -->
<div class='well'>
<h4>Leave a Comment:</h4>
<form id='form'method='POST'action='php/insert-comment.php'>
<input type='hidden' name='post_id' value='$id_post'>
<input type='text' id='comment-name' name='name' placeholder='Your name' />
<input type='text' id='comment-mail' name='mail' placeholder='Your e-mail adress' />
<textarea type='text' name='comment' class='the-new-com' rows='3'></textarea>
<input type='submit' id='submit' class='bt-add-com' value='Submit Comment'></input>
</form>
</div>
<hr>
<div class='media comment-block'>
<a class='pull-left' href='#'>
<img class='media-object' src=' $grav_url' >
</a>
<div class='media-body'>$name
<h4 class='media-heading'>
<small>$date</small>
</h4>
$comment
</div>
</div>";
$resultcomments = mysql_query("SELECT * FROM `comment` WHERE post_id = '$id_post'") or die(mysql_error());
while($affcom = mysql_fetch_assoc($resultcomments)){
$name = $affcom['name'];
$email = $affcom['mail'];
$comment = $affcom['comment'];
$date = $affcom['date'];
$default = "mm";
$size = 35;
$grav_url = "http://www.gravatar.com/avatar/".md5(strtolower(trim($email)))."?d=".$default."&s=".$size;
echo"
<!-- Posted Comments -->
<!-- Comment -->
<div class='media comment-block'>
<a class='pull-left' href='#'>
<img class='media-object' src=' $grav_url' >
</a>
<div class='media-body'>$name
<h4 class='media-heading'>
<small>$date</small>
</h4>
$comment
</div>
</div>";
}
}
?>
我正在慢慢学习PHP,所以对我来说很容易。我觉得这个问题很容易解决,而且它就在那里我无法弄清楚它是什么。