title: Php : How can I make values that have been saved in a table appear on the page as being checked?
body: <p>I have a table in my app, <code>contacts</code>, where a user (<code>user_id</code>) has a list of contacts :</p>
  
  <pre><code>contact_auto_inc      user_id       contact_id
  17                       2             7
  18                       2             8
  19                       2             9
  </code></pre>
  
  <p>I show these contacts, their corresponding names, with this code :</p>
  
  <pre><code>&lt;form action="" method="POST"&gt;
      &lt;?php
           //this code below will get the username of contacts
           // for $user_id. we get the 'contact_id'
           //values in the contacts table for $user_id, match those contact_ids to    the corresponding 
           //'user_ids' in the user table, and then show the 'usernames' for each of those user_ids
             $select_from_user_table = "SELECT  contacts.contact_id, user.username
           FROM contacts 
           INNER JOIN user
           ON contacts.contact_id=user.user_id WHERE contacts.user_id = '$user_id'";
  
          //get the result of the above
           $result2=mysqli_query($con,$select_from_user_table);
  
      //show the usernames, phone numbers
           while($row = mysqli_fetch_assoc($result2)) { ?&gt;
           &lt;input type='checkbox' name='check_contacts[]' value='&lt;?=$row['contact_id']?&gt;'&gt; &lt;?php echo $row['username'] ?&gt; &lt;/br&gt;
  
          &lt;?php } ?&gt;
  
      &lt;!--&lt;input type="submit" name = "create" value = "Create new Contact"&gt;&lt;/p&gt; --&gt;
  
      &lt;!--&lt;/form&gt; --&gt;
  
  &lt;p&gt;&lt;input type="submit" name = "Save" value = "Save"&gt;&lt;/p&gt;
  &lt;p&gt;&lt;input type="submit" name = "Delete" value = "Delete"&gt;&lt;/p&gt;
  &lt;a href="exit.php"&gt;Exit&lt;/a&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>So it looks like this : </p>
  
  <p><a href="https://i.stack.imgur.com/vCdSK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/vCdSK.png" alt="image."></a></p>
  
  <p>And if one of the boxes is checked and then saved that contact gets saved to a <code>review_shared</code> table like this :</p>
  
  <pre><code>&lt;?php
  //here we want to save the checked contacts to the review_shared table ;  that is,
  //who the user wants to share reviews with
  if(!empty($_POST['check_contacts'])) {
      foreach($_POST['check_contacts'] as $check) {
  
      $insert_review_shared_command = "INSERT INTO review_shared VALUES(NULL," .$_GET['id']. ", '$user_id','$check')";
  
          //we want to save the checked contacts into the review_shared table
          $insert_into_review_shared_table = mysqli_query($con,$insert_review_shared_command);
  
      }
  
  }
  $con-&gt;close();
  ?&gt; 
  </code></pre>
  
  <p>But whenever I go back to the page, I still see :</p>
  
  <p><a href="https://i.stack.imgur.com/uEgHm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uEgHm.png" alt="enter image description here"></a></p>
  
  <p>How would I show contacts from the <code>contacts</code> table that are also in the <code>review_shared</code> table 
  with a check in the corresponding check box ?</p>