title: Query db dynamically using HTML form as scheme
body: <p>I am a beginner with php.
  I have a data table something like this</p>
  
  <pre><code>| serial    | | name            | | email           | | phone       | | location    | | profession  | | source  |
  -----------------------------------------------------------------------------------------------------------------------------
  | 1         | | Mr first        | | a@example.com   | | 123456780   | | India       | | Designer    | | From X  |
  | 2         | | Mr second       | | b@example.com   | | 123456781   | | US          | | Designer    | | From Y  |
  | 3         | | Mr third        | | c@example.com   | | 123456782   | | US          | | Engineer    | | From X  |
  | 4         | | Mr fourth       | | d@example.com   | | 123456783   | | US          | | Disigner    | | From Z  |
  | 5         | | Mr fifth        | | e@example.com   | | 123456784   | | India       | | Engineer    | | From Y  |
  | 6         | | Mr sixth        | | f@example.com   | | 123456785   | | UK          | | Designer    | | From X  |
  | 7         | | Mr seventh      | | g@example.com   | | 123456786   | | India       | | Designer    | | From X  |
  </code></pre>
  
  <p>I have a html form like this</p>
  
  <pre><code>&lt;form method="post" action="search.php"&gt;
   &lt;select name="location"&gt;
   &lt;option value="" selected="selected"&gt;-any-&lt;/option&gt;
   &lt;option value="UK"&gt;UK&lt;/option&gt;
   &lt;option value="India"&gt;India&lt;/option&gt;
   &lt;option value="US"&gt;US&lt;/option&gt;
   &lt;/select&gt;
  
   &lt;select name="source"&gt;
   &lt;option value="" selected="selected"&gt;-any-&lt;/option&gt;
   &lt;option value="From X"&gt;From X&lt;/option&gt;
   &lt;option value="From Y"&gt;From Y&lt;/option&gt;
   &lt;option value="From Z"&gt;From Z&lt;/option&gt;
   &lt;/select&gt;
  
   &lt;select name="profession"&gt;
   &lt;option value="" selected="selected"&gt;-any-&lt;/option&gt;
   &lt;option value="Designer"&gt;Designer&lt;/option&gt;
   &lt;option value="Engineer"&gt;Engineer&lt;/option&gt;
      &lt;/select&gt;
   &lt;input type="submit" value="submit"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Now i want the query based on multiple selection</p>
  
  <ol>
  <li><p>If "any-" is selected in all three drop downs it should fetch all table rows </p></li>
  <li><p>If any two items selected i.e, location=india and profession=designer, then it should fetch only 1st and 7th row where both the select values are matching</p></li>
  </ol>
  
  <p>Please help me with the php to fetch the results based on form select values</p>
  
  <p>Here is my php to retrieve post values </p>
  
  <pre><code>&lt;?php
  
  mysql_connect("localhost","root","");
  mysql_select_db("alldata");
  
  if(isset($_POST['submit'])) {
  $source=$_POST['source'];
  $profession=$_POST['profession'];
  $location=$_POST['location'];
  }
  
  ?&gt;
  </code></pre>
  
  <p>my php file now looks like</p>
  
  <pre><code>    &lt;?php 
  $conn = mysql_connect ("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error()); 
  $selected = mysql_select_db ("alldata") 
  or die ("Could not select database because: " . mysql_error()); 
  
  if(isset($_POST['submit'])) {
  $source=$_POST['source'];
  $profession=$_POST['profession'];
  $location=$_POST['location'];
  }
  $where = '';
  if(isset($location) &amp;&amp; !empty($location)){
      $where .= "location ='$location' AND ";
  }
  if(isset($profession) &amp;&amp; !empty($profession)){
       $where .= "profession ='$profession' AND ";
  }
  if(isset($source) &amp;&amp; !empty($source)){
          $where .= "source ='$source' AND ";
  }
  $where = substr($where, 0, (strlen($where) - 4));
  $where = ($where != '') ? "WHERE $where":'';
  $sql= "select * from data $where";
  $result = mysql_query($sql,$conn)or die (mysql_error()); 
  if (mysql_num_rows($result)==0){ 
  echo "No Match Found"; 
  }else{ 
  while ($row = mysql_fetch_array($result)){ 
  echo "" .$row['name']." " .$row['email']." ".$row["phone"]." ".$row["source"]." ".$row["profession"]." ".$row["location"]."&lt;br&gt;";
  
  echo "&lt;br&gt;"; 
  echo "---------------------------------------------------------------------"."&lt;br&gt;"; 
  } 
  } 
  mysql_close(); 
  ?&gt;
  </code></pre>
  
  <p>its fetching all the rows instead of filtering based on post values. Please help</p>
  
  <p>I tried the below one. I am able to get the result by filtering values. But if there is any empty selection i need to skip that value from filtering. Here is my code</p>
  
  <pre><code>$sql = "select * from data
          where location = '".$_POST['location']."' 
          AND profession = '". $_POST['profession'] ."' 
          AND source = '". $_POST['source'] ."'";
  </code></pre>