title: foreach loop inserting all the checkboxes (checked and unchecked)
body: <p>I have a form that generates html checkboxes using php which is shown below</p>
  
  <pre><code>&lt;p&gt;&lt;form name="university" action="/university_handler" method="post"&gt;
    &lt;fieldset&gt;
      &lt;table class="table"&gt;
        &lt;thead&gt;
          &lt;tr&gt;
            &lt;th&gt;&lt;span class="help-block"&gt;University Department&lt;/span&gt;&lt;/th&gt;                            
          &lt;/tr&gt;
        &lt;/thead&gt;
        &lt;tbody&gt;  
          &lt;tr&gt; 
            &lt;td&gt;&lt;?php 
              $query = mysqli_query($db, "SELECT university_department FROM university WHERE university_id = '$university_id'") 
                  or die  ("Could not search!");
              while($row = mysqli_fetch_array($query)){
                $university_department = $row['university_department'];
                $_SESSION['university_department'] = $university_department;
                $universityDepartment = $_SESSION['university_department'];
                echo "&lt;label&gt;&lt;input type='checkbox' name='university_department[]' value='{$universityDepartment}'&gt;$universityDepartment&lt;/label&gt;&lt;br&gt;&lt;input type='text' value='' name='professor_name[{$universityDepartment}]' placeholder='Professor-Name'&gt;&lt;input type='text' value='' name='class_name[{$universityDepartment}]' placeholder='Class-Name'&gt;";}
            ?&gt;&lt;/td&gt;
          &lt;/tr&gt;
        &lt;/tbody&gt;
      &lt;/table&gt; 
      &lt;button type="submit" name="Submit"class="btn btn-info"&gt;Submit&lt;/button&gt;
    &lt;/fieldset&gt;
  &lt;/form&gt;&lt;/p&gt;
  </code></pre>
  
  <p>Now when I use my <code>university_handler</code> to insert the values into the database all of the check boxes are inserting instead of just the ones that have been checked off. I've been trying a range of things be nothing seems to be working. Here is the handler.</p>
  
  <pre><code>&lt;?php
  session_start();
  include("connect.php");
  
  $university_id = $_SESSION['university_id'];
  
  // check if share_form is submitted and not empty
  $error_message = "";
  if(is_array($_POST['university_department']) &amp;&amp; !empty($_POST['university_department'])){
    $error = array();
    $universityDepartment = $_POST['university_department'];
    if (count($universityDepartment)&gt;0){
      foreach (str_replace('#', '', $_POST['class_name']) as $departmentName =&gt; $stripid){
        $class_name_backslash = $stripid . '/';
        $class_name = mysqli_real_escape_string($db, $stripid);
        print_r($class_name);
      }
      $query_uni = ("INSERT INTO temp_list(departmentName, class_name, professor_name) VALUE ('$departmentName','$class_name', '$professor_name')");
      $q_u = mysqli_query($db, $query_uni) or die ('Error posting data');
  }
  }?&gt;
  </code></pre>