如何使mysql生成的帖子独立于自己

时间:2016-09-27 08:44:19

标签: php mysql sql commenting

我正在从名为post的表中检索帖子。我想为这段代码添加一个评论功能,问题是最新的帖子变量比其他帖子更具优势,因为如果你评论旧帖子,那么“最新帖子的变量(由于ORDER BY ID DECS标准“  有人帮我把这些帖子独立完成......谢谢

 <?php

    $sqlhash="SELECT * FROM posts ORDER BY id DESC ";
        $result_hash = mysqli_query($conn,$sqlhash);
        while($rowhash = mysqli_fetch_assoc( $result_hash))
        {
            $user = $rowhash['user'];
            $message = $rowhash['post'];
            $time =  $rowhash['time'];
        $id=  $rowhash['id'];
            $tt = date('m/d/Y H:i:s',$time);

    $my_id = $_SESSION['user_id'];




     $sql="SELECT * FROM users WHERE Full_name='$user'  ";
      $result_set = mysqli_query($conn,$sql);
      while($row = mysqli_fetch_assoc( $result_set))
      {

        $select_username = $row['Full_name'];
        $profp = $row['pic'];



    $my_id = $_SESSION['user_id'];


    ?>

<center><div class='post' style='width:48%; height:40%; align:middle; text-align:left; margin:30px 0'>
<img width="70" height="70" src='uploads/<?php echo $profp; ?>'><br><p style='color:#0c88b5'><b>  <?php echo $id ;?><br><?php echo $user ;?>On:<?php echo $tt ; ?><h4><b><?php echo $message 
;?></h4></p><hr>
<?php 

$sqlcom="SELECT * FROM comment WHERE hash='$id' ";
    $result_com = mysqli_query($conn,$sqlcom);
    while($rowcom = mysqli_fetch_assoc( $result_com))
    {
        $usercom = $rowcom['comment'];
        $comenter = $rowcom['user'];

echo "<p style='color:#0c88b5' $comenter</p>". ":" ;
 echo $usercom. "<br>" ;

 }
if (isset($_POST['commen'])) {


$sqlhash= "SELECT * FROM posts WHERE post= '$message' ";
    $result_hash = mysqli_query($conn,$sqlhash);
    while($rowhash = mysqli_fetch_assoc( $result_hash))
    {
        $user1 = $_SESSION['username'];
        $comment = $_POST['commen'];
        $time =  $rowhash['time'];



        $time2 = $_SESSION['time'];



 $sql3 = "INSERT INTO comment (user, comment, hash) VALUES
             ('$user1','$comment', '$id')";

             if($conn->query($sql3) === TRUE) {

header('Location: timeline.php');

}else {
    echo"error";
}

1 个答案:

答案 0 :(得分:0)

在post表的数据库级别进行一些更改,以管理帖子上的回复主题。

  1. 添加一个parent_id列,其默认值为null,其中包含对其进行回复或评论的帖子ID。
  2. 对于每条评论或回复,请在parent_id列中添加一个条目。
  3. 要显示帖子,请获取parent_id IS NULL 的记录。通过这个你只会得到帖子,按照id的顺序来获得正确的顺序。
  4. 对于每个帖子,您可以通过输入类似parent_id的条件来检索评论或回复 IS NOT NULL
  5. 这会对你有帮助。