title: Multiple rows being created when one row should be generated
body: <p>I have a comments feature on my social networking site. The idea is that when a user clicks on the <code>comments</code> link printed for each message, a list of existing messages are displayed as well as a form to add your own comment. I.e.</p>
  
  <p><a href="https://i.stack.imgur.com/B5T4G.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/B5T4G.png" alt="enter image description here"></a></p>
  
  <p>But, when a user adds a comment, it adds the comment to all the users posts. For example Ronda has two posts. If I add a comment to Ronda's post "Hello" (in image), the comment will also be displayed for the post with the id completely different and two rows will be generated in my <code>user_comments</code> table. If a user has more posts, I.e. Alice has 10 posts, if I add a comment to one of them, the same comment will be displayed for all 10 posts (and 10 new rows in db).</p>
  
  <p>I don't understand why it is happening. It use to work fine before, and I haven't changed anything (from what I remember). Here is my approach:</p>
  
  <pre><code>&lt;?php
  $query = mysqli_query($connect, "SELECT * FROM user_thoughts WHERE added_by='$user' AND shared ='yes' ORDER BY id DESC");
  
  while ($row = mysqli_fetch_array($query)) {
      $thought_id      = $row['id'];
      $message_content = $row['message'];
      $date_of_msg     = $row['post_details'];
      $thoughts_by     = $row['added_by'];
      $attachent       = $row['attachment'];
      $shared          = $row['shared'];
  
  echo "  &lt;div class='message_wrapper'&gt;
  
          // the actual post will be displayed here, as well as the comment link.
  
          &lt;/div&gt;";
  
  // this is where each comment is displayed 
              echo "&lt;div id='toggleComment$thought_id' class='new_comment'&gt;";
              if ($account_type2 == "user"){
                  // displaying the add new comment form ....
                  echo "
                      &lt;form action='' method='post' enctype='multipart/form-data'&gt;
                          &lt;table&gt;
                              &lt;tr&gt;
                                  &lt;td&gt;
                                      &lt;textarea id='txtarea' name='comment_msg' cols='80' maxlength='180' placeholder=' add your comment...'&gt;&lt;/textarea&gt; 
                                  &lt;/td&gt;
                                  &lt;td&gt;
                                      &lt;input id='send' type='submit' name='send_comm' value='Share'/&gt; 
                                  &lt;/td&gt;
                              &lt;/tr&gt;
                           &lt;/table&gt; &lt;hr/&gt;
                      &lt;/form&gt;";
              }
              while($comment = mysqli_fetch_assoc ($get_comm)) { 
                  $comment_id = $comment['id'];
                  $comment_body = $comment['body_of_msg']; 
                  $comment_posted_to = $comment['comment_posted_to']; 
                  $comment_posted_by = $comment['comment_posted_by']; 
                  $removed = $comment['comment_removed']; 
  
              $name_q = mysqli_query ($connect, "SELECT * FROM users WHERE username = '$comment_posted_by'");
              while($roww = mysqli_fetch_assoc ($name_q)) {   
                  $post_firstname = $roww['first_name'];
                  $post_lastname = $roww['last_name'];
  
              echo " 
              &lt;div class='new_comm' id='thoughtId".$thought_id."-childId".$comment['id']."'&gt; 
                  &lt;br/&gt;&lt;b&gt;&lt;a href = 'profile_page/$comment_posted_by'&gt; $post_firstname $post_lastname&lt;/a&gt;&lt;/b&gt;: $comment_body "; ?&gt;&lt;?php 
                  } 
              }
              echo "&lt;/div&gt;";
  /*************************/
      // here is where I process a new comment, and send it to the database.
      // sending comments to database 
      $comment = htmlentities(trim(strip_tags(@$_POST['comment_msg'])));
      $comment = mysqli_real_escape_string($connect, $comment);
  
      // if button is pressed, do this...
      if(isset($_POST['send_comm'])){
          if (!empty ($comment)){
              $insert_comment = mysqli_query ($connect, "INSERT INTO user_comments VALUES ('','$comment','$username','$user','0','$thought_id')");
              header ("Location: /profile_page/$user");
          } 
      }       
  /****************************/
  }// while closed
  
  ?&gt;
  </code></pre>
  
  <p><strong>Edit</strong>:</p>
  
  <p>I think I know what the issue is, but do not know how to resolve it.</p>
  
  <p>The problem is that <code>if(isset($_POST['send_comm'])){</code> processing is done in the while loop which displays all the posts for a user. However, if I put it outside the while loop, <code>$thought_id</code> in that code will be undefined. I have tried this approach:</p>
  
  <pre><code>while ($row = mysqli_fetch_array($query)) {
      $thought_id      = $row['id'];
  
      // if button is pressed, do this...
      if(isset($_POST['send_comm'])){
          if (!empty ($comment)){
              $insert_comment = mysqli_query ($connect, "INSERT INTO user_comments VALUES ('','$comment','$username','$user','0','$thought_id')");
              header ("Location: /profile_page/$user");
          } 
      }
  }
  </code></pre>
  
  <p>But no comment is added to the post (no row is generated, INSERT query not executed).</p>