post id是两个表中的公共字段。如果我想在同一个post_id下选择评论,那么帖子应该只显示一次,并且所有评论都在其下面。 这是我的代码......
home.php
<?php
$sql="SELECT family_news.post_id,family_news.username,family_news.post,
family_news.fileToUpload,family_news.description,
family_comments.post_id,family_comments.comment,
family_comments.commenter
FROM family_news
LEFT JOIN family_comments ON family_news.post_id = family_comments.post_id
ORDER BY family_news.post_id DESC";
if($result = mysqli_query($con, $sql))
{
while($row = mysqli_fetch_array($result))
{
?>
<div class="form-group">
<b style="font-size:30px;color:brown;">
<?php
echo $row['username'];
?>
</b>
<br>
<?php
if($row['fileToUpload'] != NULL)
{
?>
<form action="post_comment.php" method="POST">
<input class="form-control" type="hidden" name="post_id" value="<?php echo $row['post_id']; ?>">
<br>
<img class="img-responsive" src="../admin_panel/uploads/<?php echo $row['fileToUpload']; ?>" alt="Image Loading" height='80' width='120'>
<div class="form-group" style="font-size:15px;color:purple;">
<?php
echo $row['description'];
?>
<br>
<h3 style="color:black">Comments</h3>
<h3 style="color:red">
<?php
echo $row['commenter'];
?>
</h3>
<h4 style="color:saddlebrown">
<?php
echo $row['comment'];
?>
</h4>
<br>
<input class="form-control" type="text" name="comment" placeholder="Write a comment">
<br>
<input class="form-control" type="hidden" name="commenter" value="<?php echo $_SESSION['uname']; ?>">
<input type="submit" class="btn btn-primary" value="Post">
</div>
</form>
<?php
}
if($row['post'] != NULL)
{
?>
<br>
<div class="form-group" style="font-size:15px;color:purple;">
<form action="post_comment.php" method="POST">
<input class="form-control" type="hidden" name="post_id" value="<?php echo $row['post_id']; ?>">
<?php
echo $row['post'];
?>
<br>
<h3 style="color:black">Comments</h3>
<h3 style="color:red">
<?php
echo $row['commenter'];
?>
</h3>
<h4 style="color:saddlebrown">
<?php
echo $row['comment'];
?>
</h4>
<br>
<input class="form-control" type="text" name="comment" placeholder="Write a comment">
<br>
<input class="form-control" type="hidden" name="commenter" value="<?php echo $_SESSION['uname']; ?>">
<input type="submit" class="btn btn-primary" value="Post">
</form>
<?php
}
?>
</div>
<?php
}
}
?>
</div>
</section>
post_comment.php
<section class="col-lg-6 connectedSortable">
<?php
$post_id=$_POST['post_id'];
$comment=$_POST['comment'];
$commenter=$_POST['commenter'];
if($comment != NULL) {
$sql="INSERT INTO family_comments
(post_id, comment, commenter)
VALUES ('$post_id', '$comment', '$commenter')";
if($con ->query($sql) == TRUE)
{
echo 'Comment saved Successfully...';
}
} else {
echo 'Please Write Something...';
}
?>
</section>