Php 如何使我的回复显示为对评论的回复,而不是显示在底部?

Php 如何使我的回复显示为对评论的回复,而不是显示在底部?,php,html,mysql,Php,Html,Mysql,我有一个带有回复系统的评论系统,每当我回复评论时,它都会出现在底部,我必须提供所有代码,因为我不确定哪段代码是问题所在 我添加这个问题已经有一段时间了,我正在努力解决这个问题,谢谢你的帮助 这是我的comments.inc.php代码: <?php function setComments($conn) { if (isset($_POST['commentSubmit'])) { $uid = $_POST['uid']; $date = $_POST[

我有一个带有回复系统的评论系统,每当我回复评论时,它都会出现在底部,我必须提供所有代码,因为我不确定哪段代码是问题所在

我添加这个问题已经有一段时间了,我正在努力解决这个问题,谢谢你的帮助

这是我的comments.inc.php代码:

<?php
function setComments($conn) {
   if (isset($_POST['commentSubmit'])) {
       $uid = $_POST['uid'];
       $date = $_POST['date'];
       $message = $_POST['message'];
       $message = preg_replace ( 
    "/(?<!a href=\")(?<!src=\")((http|ftp)+(s)?:\/\/[^<>\s]+)/i",
    "<a href=\"\\0\" target=\"blank\">\\0</a>",
    $message
);
     $sql = "INSERT INTO comments (uid, date, message) VALUES ('".mysqli_real_escape_string($conn,$uid)."','".mysqli_real_escape_string($conn,$date)."','".mysqli_real_escape_string($conn,$message)."')";
     $result = $conn->query($sql);
     }
 } 

function getComments($conn) {
  $sql = "SELECT * FROM comments";
  $result = $conn->query($sql);
  while($row = $result->fetch_assoc()) {
    $id = $row['uid'];
    $sql2 = "SELECT * FROM users WHERE id='$id'";
    $result2 = $conn->query($sql2); 
    if ($row2 = $result2->fetch_assoc()) {
       echo "<div class='comment-box'><p>";
    echo $row2['first_name']."<br>";  
    echo $row['date']."<br>";   
    echo nl2br($row['message']);
        echo "</p>";
           if (isset($_SESSION['id'])) {
             if ($_SESSION['id'] == $row2['id']) {
           echo "<form class='delete-form' method='POST' action='".deleteComments($conn)."'>
          <input type='hidden' name='cid' value='".$row['cid']."'>
          <button type='submit' name='commentDelete'>Delete</button>
        </form>
        <form class='edit-form' method='POST' action='editcomment.php'>
          <input type='hidden' name='cid' value='".$row['cid']."'>
          <input type='hidden' name='uid' value='".$row['uid']."'>
          <input type='hidden' name='date' value='".$row['date']."'>
          <input type='hidden' name='message' value='".$row['message']."'>
          <button>Edit</button>
        </form>
        ";
          } else {
          echo "<form class='edit-form' method='POST' action='replycomment.php'>
          <input type='hidden' name='cid' value='".$row['cid']."'>
          <input type='hidden' name='uid' value='".$row['uid']."'>
          <input type='hidden' name='date' value='".$row['date']."'>
          <input type='hidden' name='reply' value='".$row['reply']."'>
          <button style='height: 90px;'><img src='img.ico' style=''></button>
        </form>";   
       }
     }  else {
       echo "<p class='commentmessage'>You need to be logged in to reply</p>";
     }
     echo "</div>"; 
    }
  }
}



function replyComments($conn) {
   if (isset($_POST['replySubmit'])) {
       $cid = $_POST['cid'];       
       $uid = $_POST['uid'];
       $date = $_POST['date'];
       $reply = $_POST['reply'];
       $first_name = $_POST['first_name'];
             $reply = preg_replace ( 
    "/(?<!a href=\")(?<!src=\")((http|ftp)+(s)?:\/\/[^<>\s]+)/i",
    "<a href=\"\\0\" target=\"blank\">\\0</a>",
    $reply
);
       $sql = "INSERT INTO replies (uid, first_name, date, reply) VALUES ('".mysqli_real_escape_string($conn,$uid)."','".mysqli_real_escape_string($conn,$first_name)."','".mysqli_real_escape_string($conn,$date)."','".mysqli_real_escape_string($conn,$reply)."')";
       $result = $conn->query($sql);
       header("Location: index1.php");
     }
 } 


 function deleteComments($conn) {
   if (isset($_POST['commentDelete'])) {
     $cid = $_POST['cid'];       

     $sql = "DELETE FROM comments WHERE cid='".mysqli_real_escape_string($conn,$cid)."'";  
     $result = $conn->query($sql);
     header("Location: index1.php");
     }
}

function editComments($conn) {
   if (isset($_POST['commentSubmit'])) {
       $cid = mysqli_real_escape_string($conn, $_POST['cid']);  
       $uid = mysqli_real_escape_string($conn, $_POST['uid']);
       $date = mysqli_real_escape_string($conn, $_POST['date']);
       $message = mysqli_real_escape_string($conn, $_POST['message']);

       $sql = "UPDATE comments SET message='".mysqli_real_escape_string($conn,$message)."' WHERE cid='".mysqli_real_escape_string($conn,$cid)."'";    
       $result = $conn->query($sql);
     header("Location: index1.php");
     }
 } 


function getLogin($conn) {
   if (isset($_POST['loginSubmit'])) {
      $email = $_POST['email'];   
      $password = md5($_POST['password']);   

     $sql = "SELECT * FROM users WHERE email='$email' AND password='$password'";
      $result = $conn->query($sql);
       if (mysqli_num_rows($result) > 0) {
         if($row = $result->fetch_assoc()) {
           $_SESSION['id'] = $row['id'];
           header("Location: index1.php?loginsuccess");
           exit();
        }   
      } else {
       header("Location: index.php?loginfailed");
       exit();
      }
    }
 } 
?> 
<?php 
  date_default_timezone_set('America/New_York');
  include 'dbh.inc.php';
  include 'comments.inc.php';
?>
<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
     <title>Comments</title>
     <link rel="stylesheet" href="style.css">
   </head>

   <body>


     <?php
       $cid = $_POST['cid'];
       $uid = $_POST['uid'];
       $date = $_POST['date'];
       $reply = $_POST['reply'];
       $first_name = $_POST['first_name'];

          echo "<form method='POST' action='".replyComments($conn)."'>
          <input type='text' name='first_name' placeholder='First Name' value='".$first_name."'>
          <br>
          <input type='hidden' name='cid' value='".$cid."'>
          <input type='hidden' name='uid' value='".$uid."'>
          <input type='hidden' name='date' value='".$date."'>
          <textarea name='reply'></textarea><br>
          <button type='submit' name='replySubmit'>Reply</button>         
     </form>";
?>

  </body>
</html>
<?php 
  date_default_timezone_set('America/New_York');
  include 'dbh.inc.php';
  include 'comments.inc.php';
  session_start();
  $statusMsg = $errorMsg = $insertValuesSQL = $errorUpload = $errorUploadType = '';
?>
<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
     <title>Comments</title>
</head>
     <style>

textarea {
  width: 400px;
  height: 80px;
  background-color: #fff;
  resize: none;
  margin-left: 2%;
  padding: 12px;
font-family: 'Lato', sans-serif;
}

button {
  width: 100px;
  height: 30px;
  background-color: green;
  border: none;
  color: #fff;
  font-weight: 400;
  cursor: pointer;
  margin-bottom: 60px;
  margin-left: %;
  font-family: 'Lato', sans-serif;
}

button:hover {
    background-color: #282828;
}

.likebtn-wrapper {
  margin-bottom: 550%;   
}

.comment-box {
  width: 845px;
  padding: 15px;
  margin-bottom: 1%;
  background-color: #fff;
  border-radius: 4px; 
  position: relative;
  font-family: 'Lato', sans-serif;
}

.comment-box p {
 font-size: 16px;
 line-height: 20px;
 color: gray;
 font-weight: 100;
 font-family: 'Lato', sans-serif;
 padding: 2px 2px;
}

.edit-form {
  position: absolute;
  top: 0px;
  right: 0px;
}

.edit-form button{
  width: 40px;
  height: 20px;
  color: #282828;
  background-color: #fff;  
  opacity: 0.7; 
}

.edit-form button:hover{
    opacity: 1; 
}

.delete-form {
  position: absolute;
  top: 0px;
  right: 60px;
}

.delete-form button{
  width: 40px;
  height: 20px;
  color: #282828;
  background-color: #fff;  
  opacity: 0.7; 
}

.delete-form button:hover{
    opacity: 1; 
}

.reply-form {
  float: left;
  top: 0px;
  right: 120px;
}

.reply-form button{
  width: 40px;
  height: 20px;
  color: #282828;
  background-color: #fff;  
  opacity: 0.7; 
}

.reply-form button:hover{
    opacity: 1; 
}

.commentmessage {
  float: right;
  position: absolute;
  top: 10px;
  right: 10px;
  font-size: 20px;
}

#myDIV {
  width: 100%;
  padding: 50px 0;
  text-align: center;
  background-color: lightblue;
  margin-top: 20px;
  display: none;
}

 html {
    margin: 0;
    padding: 0;
    background-color: #4ebd46;
    font-family: 'Montserrat', sans-serif;
}

body {
    width: 70%;
    margin: 0 auto;
    padding: 1em 50px;
    background: #feffa6;
    font-family: 'Montserrat', sans-serif;
}

.header {
  background-color: #87ea6b;
  margin: 0;
  padding-top: 6%;
  padding-bottom: -5%;
  margin-top: -2%;
  margin-left: -5.2%;
  margin-right: -5.2%;
  font-family: 'Montserrat', sans-serif;
}

h1, h2 {
  text-align: center;
  color: white;
  font-family: 'Lato', sans-serif;
}

h1 {
  font-size: 45px;
  margin-left: -18%;
  font-family: 'Lato', sans-serif;

}

.logo {
  width: 35%;
  margin-top: -20%;
}

button {
  background-color: #90bd62;
}

.first {
    margin-left: 2%;
}

a {
    cursor: pointer;
}

.edit-form button{
  width: 40px;
  height: 20px;
  color: #282828;
  opacity: 0.7; 
  margin-left: -60%;
  margin-top: 40%;
}


a:hover {
 text-decoration: underline;
}

.first {
    color: #821510;
    font-size: 17px;
}

.replyson {
    color: red;
}

.edit-form {
    color: red;
}

footer {
  padding: 0px;
  background-color: #a5dbff;
  padding: 10px;
  text-align: center;
  color: white;
  padding-bottom: -20%;
  margin-bottom: -2%;
  margin-left: -5.2%;
  margin-right: -5.2%;
}

.ooter {
  padding: 0px;
  background-color: #a5dbff;
  padding: 20px;
  text-align: center;
  color: white;
  padding-bottom: -50%;
  margin-bottom: -2%;
  margin-left: 10%;
  margin-right: 10%;
}

.term {
    color: white;
}
     </style>
   </head>

   <body>
  <br>
  <br>


 <div class="gallery">
    <?php
    include_once 'lendex.php';
     $query = $db->query("SELECT * FROM images ORDER BY id DESC");

     if($query->num_rows > 0) {
      while($row = $query->fetch_assoc()) {
        $imageURL = 'uploads/'.$row['file_name'];
  ?>
  <img src="<?php echo $imageURL; ?>" width='200' height='200' alt=""/>
<?php }
  } else { ?>
       <p>No image(s) found...</p>
<?php  } ?>
</div>
</div>

    <div class="first">
     <?php
if (isset($_SESSION['id'])) {
  echo " <form method='POST' action='".setComments($conn)."'>
          <input type='hidden' name='uid' value='".$_SESSION['id']."'>
          <input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'>
         <textarea name='message'></textarea><br> 
         <br>
           <button type='submit' name='commentSubmit' style='height: 60px;'>Comment</button>
     </form>";

 } else {
   echo "You need to be logged in to comment!
    <br><br>";  
}
 getComments($conn);
?>
</div>

<?php 
  $sql = "SELECT * FROM replies;";
  $result = mysqli_query($conn,$sql);
  $resultCheck = mysqli_num_rows($result);

  if ($resultCheck > 0) {
     while ($row = mysqli_fetch_assoc($result)) {
      echo "<div class='comment-box'><p>";
       echo $row['first_name'];
       echo "<br>";
       echo $row['date'];
       echo "<br>";
       echo $row['reply'];
       echo "</p>";
       echo "</div>";
    }
  }
?>
  </body>
</html>

如果您不希望线程太长(否则此方法将消耗您的RAM),那么您可以将注释作为线性列表获取,并在一次传递中构建注释的整个层次结构/顺序-然后您将递归地迭代这个大数组,您的回复将是注释的直接子级:

$query = 'SELECT id, parent_id, title, body, author, created FROM comments ORDER BY COALESCE(parent_id, 0), id';
$result = $pdo->query($query);
$comments = array();
$refs = array();
while($row = $result->fetch(PDO::FETCH_ASSOC))
{
  $thisref = &$refs[$row['id']];
  $thisref['title'] = $row['title'];
  $thisref['body'] = $row['body'];
  $thisref['author'] = $row['author'];
  $thisref['created'] = $row['created'];
  if($row['parent_id'] == 0) $comments[$row['id']] = &$thisref;
  else $refs[$row['parent_id']]['children'] = &$thisref;
}

为了更好地理解该方法,请阅读文章

您是否可以提供您的
回复
表map1st建议,使用参数化查询而不是构建查询,即使您使用的是
mysqli\u real\u escape\u string()
。您的代码受SQL注入的约束。存储回复时,应用该回复的注释应有外键。另外,您可以使用一个查询来获取注释,另一个查询来获取相关的用户信息。这应该是单个查询中的简单联接。如果您希望回复与它们的父注释一起列出,那么它应该是一个简单的注释连接。我将把这段代码放在哪里,是index.php还是comments.inc.php?谢谢你的回应!您将在
comments.inc.php