title: Inserting comments into table A which are linked with table B, and displaying them on the web page
body: <p>I have a function that is displaying the articles on my database.</p>
  
  <p>There are two tables, one that holds the comments and one that holds the articles. I am connecting them through a foreign key.</p>
  
  <pre><code>function show_articles() { 
      include('connection.php');
      $sql = "SELECT blog.content_id, 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.blog_id
              WHERE blog.content != ''
              ORDER BY blog.content_id DESC";
      $result = mysqli_query($dbCon, $sql);
  
      $previous_blog_id = 0;
  
      while ($row = mysqli_fetch_array($result)) {
          if ($previous_blog_id != $row['content_id']) {
              echo "&lt;h5 class='posted_by'&gt;Posted by {$row['posted_by']} on {$row['date']}&lt;/h5&gt; 
                  &lt;h1 class='content_headers'&gt;{$row['title']}&lt;/h1&gt; 
                  &lt;article&gt;{$row['content']}&lt;/article&gt;;
  
              $previous_blog_id = $row['content_id'];
          }
          if (!empty($row['comment_by']) &amp;&amp; !empty($row['comments'])) {
               echo "&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>This function is displaying the articles on the page, as well as comments for these articles. Comments are unique to the article they belong.</p>
  
  <p>Now I am using the below function to insert a new comment for a specific article.</p>
  
  <pre><code>function insert_comments($comment_by, $comment) {
      include('core/db/db_connection.php');
      $comment_by = sanitize($comment_by);
      $comment = sanitize($comment);
      $sql = "INSERT INTO article_comments (comment_by, comment)
              VALUES ('$comment_by', '$comments')";
      mysqli_query($dbCon, $sql);
  }
  </code></pre>
  
  <p>When I run the above function I get this error: </p>
  
  <pre><code>Cannot add or update a child row: a foreign key constraint fails (`DB1`.`article_comments`, CONSTRAINT `comment_blog_fk` FOREIGN KEY (`blog_id`) REFERENCES `blog` (`content_id`) ON DELETE NO ACTION ON UPDATE CASCADE) 
  </code></pre>
  
  <p>How can I insert a comment in my database that is linked to an article that the user is replying to? The foreign key field is called blog_id and it resides in article_comments table</p>
  
  <p><strong>EDIT</strong>: After applying</p>
  
  <pre><code>function insert_comments($comments, $comment_by, $blog_id) {
      include('core/db/db_connection.php');
      $comment_by = sanitize($comment_by);
      $comments = sanitize($comments);
      $sql = "INSERT INTO article_comments (comments, comment_by, blog_id)
              VALUES ('$comments', '$comment_by', '$blog_id')";
      mysqli_query($dbCon, $sql);
  }
  </code></pre>
  
  <p>How can I target $blog_id ? I run the below</p>
  
  <pre><code>&lt;?php echo list_articles(); 
      if (!empty($_POST)) {
          insert_comments($_POST['comments'], $_POST['username'], 11);
      }
  ?&gt;
  &lt;form method='post' action='' class='comments_form'&gt;
      &lt;input type='text' name='username' placeholder='your name... *' id='name'&gt;
      &lt;textarea name='comments' id='textarea' placeholder='your comment... *' cols='30' rows='6'&gt;&lt;/textarea&gt; 
      &lt;input type='submit' name='submit' id='post' value='post'&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>I have used 11 because I would like to target the 11th id of the blog article content and link it with the comments.</p>
  
  <p>How can I do this without manually inserting the number?</p>