请链接我任何教程或简单地告诉我如何做到这一点,它只显示最近添加的新闻评论。如何只选择一个?
<?php
echo "<form action='".setComments($conn)."' method='post'>
<input type='hidden' name='username'><br>
<input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'><br>
<label>Comment:<br> <textarea cols='50' rows='6' name='message' ></textarea></label><br>
<button type='submit' name='commentSubmit' class='btn btn-default'>Comment</button>
</form>";
getComments($conn);
?>
<div class="comment_div">
<?php
$query = "SELECT * FROM comment order by date desc";
mysqli_query($conn,$query) or die("Error queryng db");
$result = mysqli_query($conn,$query);
while ($row = mysqli_fetch_array($result)){
$name = $row['name'];
$com = $row['comment'];
$date = $row['date'];
echo "
<div class='commentinfo'>
<p class='username'>სახელი: $name ; თარიღი: $date</p>
<p class='comment'>კომენტარი: <br> $com</p>
</div>
";
}
?>在comments.php中:
<?php
function setComments($conn) {
if (isset($_POST['commentSubmit'])) {
$uid = $_POST['username'];
$date = $_POST['date'];
$message = $_POST['message'];
$sql = "INSERT INTO comments (username,date,message) VALUES ('$uid','$date', '$message')";
$result = mysqli_query($conn,$sql);
}
}
function getComments ($conn) {
$sql = "SELECT * FROM comments";
$result = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_array($result)) {
echo $row['username']."<br>";
echo $row['date']."<br>";
echo $row['message']."<br><br>";
};
echo "
<form method='post' action'editcomment.php'>
<input type='hidden' name='id' value='".$row['id']."'>
<input type='hidden' name='username' value='".$row['username']."'>
<input type='hidden' name='date' value='".$row['date']."'>
<input type='hidden' name='message' value='".$row['message']."'>
<button>შესწორება</button>
";
}我只想显示新闻张贴的评论,我要所有的新闻评论其他新闻。
发布于 2017-09-09 22:52:15
您需要在查询中使用LIMIT和ORDER BY标记,因此它将如下所示:
$sql = "SELECT * FROM comments ORDER BY id DESC LIMIT 1";这个查询有两个新参数,我们需要按ID对它进行排序,这样就可以得到降序(这就是DESC的用途),并且我们将数字限制为1;如果需要更多,可以更改数字。
如果您有一个想要拉取的特定项目,可以在查询中使用WHERE子句:WHERE id = #call for id#
为了确保评论只出现在文章中,你需要为你的评论创建某种标识符行,并在你的帖子中进行比较:
// get the post ID from the database
$post_id = $row['id'];
// now that you have the post id, and assuming you have an identifier add the WHERE clause
$sql = "SELECT * FROM comments WHERE comment_post_id = " . $post_id;要添加comment_post_id,请在他们输入评论时添加他们所在的当前帖子中的内容。
因此,如果你发布的id是12,那么在该帖子中发表的评论的comment_post_id应该等于12。
发布于 2017-09-09 22:52:38
您的select查询缺少where子句。它应该看起来像。
$sql ="select * from comments where news_id='".$news_id."';https://stackoverflow.com/questions/46131584
复制相似问题