title: Multiple Checkbox Filter in PHP
body: <p>I have been trying to figure this out for a while, but am not getting anywhere with it. I have read a lot of the other questions posted here and across the internet but am not being able to find a solution. Specially in the case of using multiple checkboxes.</p>
  
  <p>Please bear with me as I try to explain the problem I am facing. </p>
  
  <p>On a page I have a list of People with their corresponding location which i pull from a database using php/sql. What I am trying to achieve is to be able to filter the list according to location using checkboxes. </p>
  
  <p>This is the code I have currently for the checkboxes.</p>
  
  <pre><code>&lt;ul&gt;
      &lt;li&gt;&lt;input type="checkbox" name="check_list[]" value="sydney"&gt;Sydney&lt;/li&gt;
      &lt;li&gt;&lt;input type="checkbox" name="check_list[]" value="durban"&gt;Durban&lt;/li&gt;
      &lt;li&gt;&lt;input type="checkbox" name="check_list[]" value="delhi"&gt;Delhi&lt;/li&gt;
      &lt;li&gt;&lt;input type="checkbox" name="check_list[]" value="cairo"&gt;Cairo&lt;/li&gt;
      &lt;li&gt;&lt;input type="checkbox" name="check_list[]" value="madrid"&gt;Madrid&lt;/li&gt;
      &lt;li&gt;&lt;input type="submit"&gt;&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>This is the code I am using to check if a checkbox is checked and accordingly modify the SQL query</p>
  
  <pre><code>if (empty ($_POST['check_list'])) {
  
      $SQLquery = 'SELECT * FROM `people`';
      $query = $conn-&gt;query($SQLquery);
  
  } elseif (!empty($_POST['check_list'])) {
  
      foreach($_POST['check_list'] as $check)
  
      $location = $check;
  
      $query = $conn-&gt;prepare('SELECT * FROM `people` WHERE `p_location` = :location');
      $query-&gt;execute(array('location' =&gt; $location));
  
  }
  </code></pre>
  
  <p>Now this code works fine when only one checkbox is checked. However, when multiple checkboxes are checked this fails. It just shows nothing.</p>
  
  <p>I know i can modify the SQL query using the <code>OR</code> operator to add other locations like so:</p>
  
  <pre><code>$query = $conn-&gt;prepare('SELECT * FROM `people` WHERE `p_location` = 'Delhi' OR `p_location` = 'Madrid')
  </code></pre>
  
  <p>However, I am unable to figure out how I can first check which all checkboxes are checked and then create the SQL statement in a way which will filter the results when there is more than one city selected.</p>
  
  <p>I am also wondering if this is the best way to achieve something like this or not. I am open to alternative ways of achieving this within the scope of php/sql.</p>