title: Displaying comments under specific posts
body: <p>im trying to assign a list of posts with a comment section that is unique to that post. My problem is that instead of displaying specific comments under specific posts, all the comments are displayed on every single post.</p>
  
  <p>Im using 2 database tables: "posts: id, title, subtitle, content" 
  and "comments: comment_id, post_fk (foreign key attached to the posts id), uid, date, message.</p>
  
  <p><strong>blog.php, where posts are echoed and comments are submitted and then posted through getComments</strong></p>
  
  <pre><code>// fetch data from posts and echo them into a post
  
  $sql = "SELECT * FROM posts ORDER BY id DESC";
  $result = mysqli_query($conn,$sql);
  
  while ($row = mysqli_fetch_array($result)) {
      $title = $row['title'];
      $subtitle = $row['subtitle'];
      $content = $row['content'];
  ?&gt;
      &lt;h2&gt;&lt;?php echo $title; ?&gt; - &lt;small&gt;&lt;?php echo $subtitle; ?&gt;&lt;/small&gt;&lt;/h2&gt;
      &lt;p&gt;&lt;?php echo $content; ?&gt;&lt;/p&gt;
  
  // echo the comment field and then get comments from the getComments function echo
  echo    "&lt;form method='posts' action='".setComments($conn)."'&gt;
              &lt;input type='hidden' name='uid' value='Anonymous'&gt;
              &lt;input type='hidden' name='date' value='".date('Y-m-d H:i:s')."'&gt;
              &lt;textarea name='message'&gt;&lt;/textarea&gt;&lt;br&gt;
              &lt;button type='submit' name='commentSubmit'&gt;Comment&lt;/button&gt;
      &lt;/form&gt;";
  
          getComments($conn);
  </code></pre>
  
  <p><strong>comments.php, where the setComments and getComments functions are defined</strong></p>
  
  <pre><code>function setComments($conn) {
  if (isset($_POST['commentSubmit'])) {
  
      $uid = $_POST['uid'];
      $date = $_POST['date'];
      $message = $_POST['message'];
  
  
      $sql = "INSERT INTO comments (uid, date, message) VALUES ('$uid', '$date', '$message')";
      $result = mysqli_query($conn, $sql);
      }
  }
  
  function getComments($conn) {
      $sql = "SELECT
      c.uid, 
      c.date,
      c.message 
  FROM comments AS c  
      LEFT JOIN posts AS p ON
      c.post_fk = p.id
      WHERE c.post_fk = p.id
  ";
      $result = mysqli_query($conn, $sql);
      while ($row = $result-&gt;fetch_assoc()) {
          echo $row['uid'];
          echo $row['date']."&lt;br&gt;&lt;br&gt;";
          echo $row['message']."&lt;br&gt;&lt;br&gt;";
  </code></pre>
  
  <p>I have been experimenting with the LEFT JOIN without any promising results. Im very new to PHP, but im suspecting that I have to add some additional values to the comment form that determines which post the comment belongs to. I hope this information is substantial enough and sorry if my code is messy! </p>
  
  <p>Thank you</p>
  
  <p>(edit)
  <strong>setcomment.php</strong></p>
  
  <pre><code>function setComments($conn) {
  if (isset($_POST['commentSubmit'])) {
      $comment_id = $_POST['comment_id'];
      $uid = $_POST['uid'];
      $date = $_POST['date'];
      $message = $_POST['message'];
  
  
      $sql = "INSERT INTO comments (comment_id, uid, date, message) VALUES ($comment_id, $uid, $date, $message)";
      $stmt = mysqli_prepare($sql);
      mysqli_stmt_bind_param($stmt, "iiss", $id, $uid, $date, $message);
      $result = mysqli_stmt_execute($stmt);
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/ueEFr.png" rel="nofollow noreferrer">comments table</a>
  <a href="https://i.stack.imgur.com/oO2jb.png" rel="nofollow noreferrer">posts table</a></p>