请链接我任何教程或只是告诉我如何这样做,它只显示最近添加的新闻评论。我如何只选择一个?
<?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>
";
}
我想只显示发布评论的新闻,我要发布所有新闻评论其他新闻。
答案 0 :(得分:0)
您需要在查询中使用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。
答案 1 :(得分:0)
您的选择查询缺少where子句。它应该看起来像。
$sql ="select * from comments where news_id='".$news_id."';