title: Several multiple select (HTML) search with PHP and SQL
body: <p>I'm stuck in a piece of code that I can't make it work. I need help. Here it goes:</p>
  
  <p>I want to construct a search with several multiselect, PHP and SQL. In the exemple I'll use 02 (two) multiselects:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;form action="" method="post"&gt;
  &lt;!-- first select with its options--&gt;
  &lt;select name="search[field1]" multiple="multiple"&gt;
    &lt;option value="All"&gt;-- All --&lt;/option&gt;
    &lt;option value="value11"&gt;Value 11&lt;/option&gt;
    &lt;option value="value12"&gt;Value 12&lt;/option&gt;
    &lt;option value="value13"&gt;Value 13&lt;/option&gt;
    &lt;option value="value14"&gt;Value 14&lt;/option&gt;
  &lt;/select&gt;
    
  &lt;!-- second select with its options --&gt;
  &lt;select name="search[field2]" multiple="multiple"&gt;
    &lt;option value="All"&gt;-- All --&lt;/option&gt;
    &lt;option value="value21"&gt;Value 21&lt;/option&gt;
    &lt;option value="value22"&gt;Value 22&lt;/option&gt;
    &lt;option value="value23"&gt;Value 23&lt;/option&gt;
    &lt;option value="value24"&gt;Value 24&lt;/option&gt;
  &lt;/select&gt;
    
  &lt;!-- input submit --&gt;
  &lt;input type="submit" value="Search" name="submit"/&gt;
    
  &lt;/form&gt;&lt;!-- \. end form --&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Now, I submit the form to the PHP which it's in the same page as the form:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?php
  
  #first I check if the form was submitted
  if(isset($_POST['submit'])){
  
    #catch the values from the multiple select forms
    $mapping = array (
        'field1' =&gt; 'Name_of_the_table_field_1' , 
        'field2' =&gt; 'Name_of_the_table_field_2'
    );
  
    $stmt = "select * from [DATA_BASE].[dbo].[TABLE] ";
    $operator = " AND ";
  
    if (isset($_POST['search'])){
      $stmt = $stmt . "where ";
      foreach ($_POST['search'] as $key=&gt;$val) {
        $stmt .= $mapping[$key] ." =(case when ".$val." ='All' then ".$mapping[$key]."    else  '".$val."' end)" . $operator; 
      }
  }
  
  var_dump($stmt);
  
  
  ?&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>Here, I stuck! I don't know how to catch multiple values from the two multiple select at the same time and parsing them to a SQL search.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>E.g.: the search should return sth like "select * from [DATA_BASE].[dbo].[TABLE] where Name_of_the_table_field_1 = (case when Name_of_the_table_field_1 = 'All' then Name_of_the_table_field_1 else 'value11' AND "value12" end) AND Name_of_the_table_field_2 = (case when Name_of_the_table_field_2 = 'All' then Name_of_the_table_field_2 else 'value21' AND "value23" end)</code></pre>
  </div>
  </div>
  </p>
  
  <p>I could handle that by treating the multiselects one by one, but this would become a big chunk of code at the end, also it would be very difficult to understand.</p>
  
  <p>I hope you guys have understood me, I really need help!</p>
  
  <p>Thanks in advance!!! </p>
  
  <p>PS.: the code is catching the first option select in each multiselect, but it's not holding the others.</p>