title: Displaying and linking Foreign Key content in PHP
body: <p>I am taking some online courses and in one of the exercises we are to create two tables for a blog - blog articles and blog posts - and connect them through a foreign key, then display all content from both. Comments should be linked to a specific article only, while also allowing multiple comments.</p>
  
  <p>My attempt:</p>
  
  <pre><code>function list_articles() { 
      include('core/db/db_connection.php');
      $sql = "SELECT blog.title, blog.content, blog.posted_by, blog.date, article_comments.comments, article_comments.comment_by
              FROM blog LEFT OUTER JOIN article_comments
              ON blog.content_id = article_comments.content_id
              WHERE blog.content != ''
              ORDER BY blog.content_id DESC";
      $result = mysqli_query($dbCon, $sql);
      while ($row = mysqli_fetch_array($result)) {
          echo 
              "&lt;h5 class='posted_by'&gt;Posted by " . $posted_by = $row['posted_by'] . " on " . $row['date'] . "&lt;/h5&gt;" . 
              "&lt;h1 class='content_headers'&gt;" . $title = $row['title'] . "&lt;/h1&gt;" . 
              "&lt;article&gt;" . $content = $row['content'] . "&lt;/article&gt;" . 
              "&lt;div class='commented_by'&gt;Posted by: " . $row['comment_by'] . "&lt;/div&gt;" . 
              "&lt;div class='comments'&gt;Comments: " . $row['comments'] . "&lt;/div&gt;";
      }
  }
  </code></pre>
  
  <p>And this is how I'm inserting comments in the database:</p>
  
  <pre><code>function insert_comments($comment_by, $comments) {
      include('core/db/db_connection.php');
      $sql =  "SELECT blog.content_id, article_comments.blog_id  
               FROM blog AS blog
               INNER JOIN article_comments AS article_comments ON article_comments.blog_id &gt; blog.content_id";
      mysqli_query($dbCon, $sql);
  }
  </code></pre>
  
  <p>IN PHPMyAdmin the foreign key works alright and the comments are linked to a specific article. I want to transpose this on a web page. When I insert a new article on the page it works alright, but when I try to insert a comment for that article it will not display it.</p>
  
  <p>If I change <code>ON blog.content_id = article_comments.content_id</code> to <code>ON blog.content_id = article_comments.blog_id</code> (blog_id is the field name for the foreign key) - it will display all the comments for an article - but it duplicates that article for each comment associated with it. Does that make any sense? I tried explaining it as best as I can.. Please let me know if you need further clarification. Thanks</p>
  
  <p>By the way, this is the statement I used to create the foreign key:</p>
  
  <p><code>ALTER TABLE article_comments ADD CONSTRAINT comment_blog_fk FOREIGN KEY (blog_id) REFERENCES wt.blog(content_id) ON DELETE NO ACTION ON UPDATE CASCADE;
  </code></p>
  
  <p><strong>EDIT</strong>: The result I get with <code>ON blog.content_id = article_comments.blog_id</code></p>
  
  <pre><code>Article title: LOREM IPSUM
  Content: LOREM IPSUM DOLOR SIT AMET....
  --------------------------------------
  Name: DSK
  Comment: Great article!
  
  -- HERE IT DUPLICATES THE ARTICLE TO INSERT A NEW COMMENT --
  
  Article title: LOREM IPSUM
  Content: LOREM IPSUM DOLOR SIT AMET....
  --------------------------------------
  Name: DSK
  Comment: Great article! - 2nd comment
  </code></pre>
  
  <p>As you can see, it duplicates the article for every comment inserted. So I end up with two duplicate articles that hold different comments. If If I'll have 100 comments, the article will get replicated 100 times</p>
  
  <p>The behavior I am expecting:</p>
  
  <pre><code>Article title: LOREM IPSUM
  Content: LOREM IPSUM DOLOR SIT AMET....
  -------------------------------------- \\ COMMENTS \\
  Name: DSK
  Comment: Great article!
  --------------------------------------
  Name: DSK
  Comment: Great article! - 2nd comment
  </code></pre>