title: Ignore empty values from multipe dropdown lists in PHP/MySQL
body: <p>I am working on a form that has 19 different drop down lists. Each list has the same 39 options in it. I'd like users to be able to select options in one or more of the lists and ignore the others. Ideally, the query would return rows from the MySQL database that match ALL the selected options in the corresponding columns in the database.</p>
  
  <p>Ex: </p>
  
  <p><code>Phoneme 1: AA     &lt;--- choice in a dropdown list of 39 items</code></p>
  
  <p><code>Phoneme 2:        &lt;--- choice in a dropdown list of 39 items</code></p>
  
  <p><code>Phoneme 3: CH     &lt;--- choice in a dropdown list of 39 items</code></p>
  
  <p><code>Phoneme 4:        &lt;--- choice in a dropdown list of 39 items</code></p>
  
  <p>When they hit submit, they would receive all words in the database that have BOTH the phoneme AA in the first position AND the phoneme CH in the third position. But I can't seem to figure out how to ignore the options they don't select.  In the example, they left phonemes 2 and 4 blank. How can I have my query automatically only try to search for the ones they have chosen?</p>
  
  <p>Thank you in advance for your help. The code I've been working with is below. Currently I get no results, although the database has over 130,000 words in it, each with their own phonetic makeup.</p>
  
  <pre><code>&lt;?php
  
  include 'db_connect.php';
  
  //submitted form variable definitions
  $phon1=$_POST['phon1'];
  $phon2=$_POST['phon2'];
  $phon3=$_POST['phon3'];
  $phon4=$_POST['phon4'];
  $phon5=$_POST['phon5'];
  $phon6=$_POST['phon6'];
  $phon7=$_POST['phon7'];
  $phon8=$_POST['phon8'];
  $phon9=$_POST['phon9'];
  $phon10=$_POST['phon10'];
  $phon11=$_POST['phon11'];
  $phon12=$_POST['phon12'];
  $phon13=$_POST['phon13'];
  $phon14=$_POST['phon14'];
  $phon15=$_POST['phon15'];
  $phon16=$_POST['phon16'];
  $phon17=$_POST['phon17'];
  $phon18=$_POST['phon18'];
  $phon19=$_POST['phon19'];
  
  //-query  the database table
  
  $sql = '';
  if ($phon1!=0) {
      $sql .= ' AND phon1 LIKE ' . $phon1;
  }
  
  if ($phon2!=0) {
      $sql .= ' AND phon2 LIKE ' . $phon2;
  }
  
  if ($phon3!=0) {
      $sql .= ' AND phon3 LIKE ' . $phon3;
  }
  
  if ($phon4!=0) {
      $sql .= ' AND phon4 LIKE ' . $phon4;
  }
  
  if ($phon5!=0) {
      $sql .= ' AND phon5 LIKE ' . $phon5;
  }
  
  if ($phon6!=0) {
      $sql .= ' AND phon6 LIKE ' . $phon6;
  }
  
  if ($phon7!=0) {
      $sql .= ' AND phon7 LIKE ' . $phon7;
  }
  
  if ($phon8!=0) {
      $sql .= ' AND phon8 LIKE ' . $phon8;
  }
  
  if ($phon9!=0) {
      $sql .= ' AND phon9 LIKE ' . $phon9;
  }
  
  if ($phon10!=0) {
      $sql .= ' AND phon10 LIKE ' . $phon10;
  }
  
  if ($phon11!=0) {
      $sql .= ' AND phon11 LIKE ' . $phon11;
  }
  
  if ($phon12!=0) {
      $sql .= ' AND phon12 LIKE ' . $phon12;
  }
  
  if ($phon13!=0) {
      $sql .= ' AND phon13 LIKE ' . $phon13;
  }
  
  if ($phon14!=0) {
      $sql .= ' AND phon14 LIKE ' . $phon14;
  }
  
  if ($phon15!=0) {
      $sql .= ' AND phon15 LIKE ' . $phon15;
  }
  
  if ($phon16!=0) {
      $sql .= ' AND phon16 LIKE ' . $phon16;
  }
  
  if ($phon17!=0) {
      $sql .= ' AND phon17 LIKE ' . $phon17;
  }
  
  if ($phon18!=0) {
      $sql .= ' AND phon18 LIKE ' . $phon18;
  }
  
  if ($phon19!=0) {
      $sql .= ' AND phon19 LIKE ' . $phon19;
  }
  
  
  //-run  the query against the mysql query function 
  $result = mysql_query('SELECT * FROM bigdic WHERE 1=1' . $sql . 'LIMIT 0,100');
  $num = mysql_numrows($result);
  
  mysql_close();
  ?&gt;
  
  &lt;b&gt;Search Results:&lt;/b&gt;
  &lt;table border="1" cellspacing="2" cellpadding="2"&gt;
      &lt;tr&gt;
          &lt;th&gt;Word&lt;/th&gt;
          &lt;th&gt;Ph1&lt;/th&gt;
          &lt;th&gt;Ph2&lt;/th&gt;
          &lt;th&gt;Ph3&lt;/th&gt;
          &lt;th&gt;Ph4&lt;/th&gt;
          &lt;th&gt;Ph5&lt;/th&gt;
          &lt;th&gt;Ph6&lt;/th&gt;
          &lt;th&gt;Ph7&lt;/th&gt;
          &lt;th&gt;Ph8&lt;/th&gt;
          &lt;th&gt;Ph9&lt;/th&gt;
          &lt;th&gt;Ph10&lt;/th&gt;
          &lt;th&gt;Ph11&lt;/th&gt;
          &lt;th&gt;Ph12&lt;/th&gt;
          &lt;th&gt;Ph13&lt;/th&gt;
          &lt;th&gt;Ph14&lt;/th&gt;
          &lt;th&gt;Ph15&lt;/th&gt;
          &lt;th&gt;Ph16&lt;/th&gt;
          &lt;th&gt;Ph17&lt;/th&gt;
          &lt;th&gt;Ph18&lt;/th&gt;
          &lt;th&gt;Ph19&lt;/th&gt;
      &lt;/tr&gt;
  
  &lt;?
  //setting up the loop
  $i=0;
  while($i&lt;$num) {
  
      $word=mysql_result($result,$i,"word");
      $ph1=mysql_result($result,$i,"ph1");
      $ph2=mysql_result($result,$i,"ph2");
      $ph3=mysql_result($result,$i,"ph3");
      $ph4=mysql_result($result,$i,"ph4");
      $ph5=mysql_result($result,$i,"ph5");
      $ph6=mysql_result($result,$i,"ph6");
      $ph7=mysql_result($result,$i,"ph7");
      $ph8=mysql_result($result,$i,"ph8");
      $ph9=mysql_result($result,$i,"ph9");
      $ph10=mysql_result($result,$i,"ph10");
      $ph11=mysql_result($result,$i,"ph11");
      $ph12=mysql_result($result,$i,"ph12");
      $ph13=mysql_result($result,$i,"ph13");
      $ph14=mysql_result($result,$i,"ph14");
      $ph15=mysql_result($result,$i,"ph15");
      $ph16=mysql_result($result,$i,"ph16");
      $ph17=mysql_result($result,$i,"ph17");
      $ph18=mysql_result($result,$i,"ph18");
      $ph19=mysql_result($result,$i,"ph19");
  ?&gt;
  
      &lt;tr&gt;
      &lt;td&gt;&lt;B&gt;&lt;? echo $word; ?&gt;&lt;/B&gt;&lt;/td&gt;
      &lt;td&gt;&lt;? echo $ph1; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;? echo $ph2; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;? echo $ph3; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;? echo $ph4; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;? echo $ph5; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;? echo $ph6; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;? echo $ph7; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;? echo $ph8; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;? echo $ph9; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;? echo $ph10; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;? echo $ph11; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;? echo $ph12; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;? echo $ph13; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;? echo $ph14; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;? echo $ph15; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;? echo $ph16; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;? echo $ph17; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;? echo $ph18; ?&gt;&lt;/td&gt;
      &lt;td&gt;&lt;? echo $ph19; ?&gt;&lt;/td&gt;
      &lt;/tr&gt; 
  
  &lt;?
      $i++;
  }
  
  echo "&lt;/table&gt;";
  
  ?&gt;
  
  &lt;b&gt;&lt;? echo $num; ?&gt;&lt;/b&gt; records returned.`
  </code></pre>
  
  <p>HTML FORM:</p>
  
  <pre><code>&lt;form method="post" action="search2.php?go" id="searchform2"&gt;
      Phoneme 01: &lt;select name="phon1"&gt;
          &lt;option value="0" selected&gt;&lt;/option&gt;
          &lt;option value="AA"&gt;AA&lt;/option&gt;
          &lt;option value="AE"&gt;AE&lt;/option&gt;
          &lt;option value="AH"&gt;AH&lt;/option&gt;
          &lt;option value="AO"&gt;AO&lt;/option&gt;
          &lt;option value="AW"&gt;AW&lt;/option&gt;
          &lt;option value="AY"&gt;AY&lt;/option&gt;
          &lt;option value="B"&gt;B&lt;/option&gt;
          &lt;option value="CH"&gt;CH&lt;/option&gt;
          &lt;option value="D"&gt;D&lt;/option&gt;
          &lt;option value="DH"&gt;DH&lt;/option&gt;
          &lt;option value="EH"&gt;EH&lt;/option&gt;
          &lt;option value="ER"&gt;ER&lt;/option&gt;
          &lt;option value="EY"&gt;EY&lt;/option&gt;
          &lt;option value="F"&gt;F&lt;/option&gt;
          &lt;option value="G"&gt;G&lt;/option&gt;
          &lt;option value="HH"&gt;HH&lt;/option&gt;
          &lt;option value="IH"&gt;IH&lt;/option&gt;
          &lt;option value="IY"&gt;IY&lt;/option&gt;
          &lt;option value="JH"&gt;JH&lt;/option&gt;
          &lt;option value="K"&gt;K&lt;/option&gt;
          &lt;option value="L"&gt;L&lt;/option&gt;
          &lt;option value="M"&gt;M&lt;/option&gt;
          &lt;option value="N"&gt;N&lt;/option&gt;
          &lt;option value="NG"&gt;NG&lt;/option&gt;
          &lt;option value="OW"&gt;OW&lt;/option&gt;
          &lt;option value="OY"&gt;OY&lt;/option&gt;
          &lt;option value="P"&gt;P&lt;/option&gt;
          &lt;option value="R"&gt;R&lt;/option&gt;
          &lt;option value="S"&gt;S&lt;/option&gt;
          &lt;option value="SH"&gt;SH&lt;/option&gt;
          &lt;option value="T"&gt;T&lt;/option&gt;
          &lt;option value="TH"&gt;TH&lt;/option&gt;
          &lt;option value="UH"&gt;UH&lt;/option&gt;
          &lt;option value="UW"&gt;UW&lt;/option&gt;
          &lt;option value="V"&gt;V&lt;/option&gt;
          &lt;option value="W"&gt;W&lt;/option&gt;
          &lt;option value="Y"&gt;Y&lt;/option&gt;
          &lt;option value="Z"&gt;Z&lt;/option&gt;
          &lt;option value="ZH"&gt;ZH&lt;/option&gt;
      &lt;/select&gt;&lt;br \&gt;
  
      Phoneme 02: &lt;select name="phon2"&gt;
          &lt;option value="0" selected&gt;&lt;/option&gt;
          &lt;option value="AA"&gt;AA&lt;/option&gt;
          &lt;option value="AE"&gt;AE&lt;/option&gt;
          &lt;option value="AH"&gt;AH&lt;/option&gt;
          &lt;option value="AO"&gt;AO&lt;/option&gt;
          &lt;option value="AW"&gt;AW&lt;/option&gt;
          &lt;option value="AY"&gt;AY&lt;/option&gt;
          &lt;option value="B"&gt;B&lt;/option&gt;
          &lt;option value="CH"&gt;CH&lt;/option&gt;
          &lt;option value="D"&gt;D&lt;/option&gt;
          &lt;option value="DH"&gt;DH&lt;/option&gt;
          &lt;option value="EH"&gt;EH&lt;/option&gt;
          &lt;option value="ER"&gt;ER&lt;/option&gt;
          &lt;option value="EY"&gt;EY&lt;/option&gt;
          &lt;option value="F"&gt;F&lt;/option&gt;
          &lt;option value="G"&gt;G&lt;/option&gt;
          &lt;option value="HH"&gt;HH&lt;/option&gt;
          &lt;option value="IH"&gt;IH&lt;/option&gt;
          &lt;option value="IY"&gt;IY&lt;/option&gt;
          &lt;option value="JH"&gt;JH&lt;/option&gt;
          &lt;option value="K"&gt;K&lt;/option&gt;
          &lt;option value="L"&gt;L&lt;/option&gt;
          &lt;option value="M"&gt;M&lt;/option&gt;
          &lt;option value="N"&gt;N&lt;/option&gt;
          &lt;option value="NG"&gt;NG&lt;/option&gt;
          &lt;option value="OW"&gt;OW&lt;/option&gt;
          &lt;option value="OY"&gt;OY&lt;/option&gt;
          &lt;option value="P"&gt;P&lt;/option&gt;
          &lt;option value="R"&gt;R&lt;/option&gt;
          &lt;option value="S"&gt;S&lt;/option&gt;
          &lt;option value="SH"&gt;SH&lt;/option&gt;
          &lt;option value="T"&gt;T&lt;/option&gt;
          &lt;option value="TH"&gt;TH&lt;/option&gt;
          &lt;option value="UH"&gt;UH&lt;/option&gt;
          &lt;option value="UW"&gt;UW&lt;/option&gt;
          &lt;option value="V"&gt;V&lt;/option&gt;
          &lt;option value="W"&gt;W&lt;/option&gt;
          &lt;option value="Y"&gt;Y&lt;/option&gt;
          &lt;option value="Z"&gt;Z&lt;/option&gt;
          &lt;option value="ZH"&gt;ZH&lt;/option&gt;
      &lt;/select&gt;&lt;br \&gt;
  
      Phoneme 03: &lt;select name="phon3"&gt;
          &lt;option value="0" selected&gt;&lt;/option&gt;
          &lt;option value="AA"&gt;AA&lt;/option&gt;
          &lt;option value="AE"&gt;AE&lt;/option&gt;
          &lt;option value="AH"&gt;AH&lt;/option&gt;
          &lt;option value="AO"&gt;AO&lt;/option&gt;
          &lt;option value="AW"&gt;AW&lt;/option&gt;
          &lt;option value="AY"&gt;AY&lt;/option&gt;
          &lt;option value="B"&gt;B&lt;/option&gt;
          &lt;option value="CH"&gt;CH&lt;/option&gt;
          &lt;option value="D"&gt;D&lt;/option&gt;
          &lt;option value="DH"&gt;DH&lt;/option&gt;
          &lt;option value="EH"&gt;EH&lt;/option&gt;
          &lt;option value="ER"&gt;ER&lt;/option&gt;
          &lt;option value="EY"&gt;EY&lt;/option&gt;
          &lt;option value="F"&gt;F&lt;/option&gt;
          &lt;option value="G"&gt;G&lt;/option&gt;
          &lt;option value="HH"&gt;HH&lt;/option&gt;
          &lt;option value="IH"&gt;IH&lt;/option&gt;
          &lt;option value="IY"&gt;IY&lt;/option&gt;
          &lt;option value="JH"&gt;JH&lt;/option&gt;
          &lt;option value="K"&gt;K&lt;/option&gt;
          &lt;option value="L"&gt;L&lt;/option&gt;
          &lt;option value="M"&gt;M&lt;/option&gt;
          &lt;option value="N"&gt;N&lt;/option&gt;
          &lt;option value="NG"&gt;NG&lt;/option&gt;
          &lt;option value="OW"&gt;OW&lt;/option&gt;
          &lt;option value="OY"&gt;OY&lt;/option&gt;
          &lt;option value="P"&gt;P&lt;/option&gt;
          &lt;option value="R"&gt;R&lt;/option&gt;
          &lt;option value="S"&gt;S&lt;/option&gt;
          &lt;option value="SH"&gt;SH&lt;/option&gt;
          &lt;option value="T"&gt;T&lt;/option&gt;
          &lt;option value="TH"&gt;TH&lt;/option&gt;
          &lt;option value="UH"&gt;UH&lt;/option&gt;
          &lt;option value="UW"&gt;UW&lt;/option&gt;
          &lt;option value="V"&gt;V&lt;/option&gt;
          &lt;option value="W"&gt;W&lt;/option&gt;
          &lt;option value="Y"&gt;Y&lt;/option&gt;
          &lt;option value="Z"&gt;Z&lt;/option&gt;
          &lt;option value="ZH"&gt;ZH&lt;/option&gt;
      &lt;/select&gt;&lt;br \&gt;    
  
      &lt;input type="submit" name="submit" value="Search"&gt;
  &lt;/form&gt;
  </code></pre>