title: Search Filtering with PHP/MySQL
body: <p>I'm trying to create a search/filtering option in my blood donor application. Where donor can be searched by sex, name, blood group or by selecting all three. Here is my code</p>
  
  <pre><code>function search_donar($_POST) {
  
          $by_name = $_POST['by_name'];
          $by_sex = $_POST['by_sex'];
          $by_group = $_POST['by_group'];
          $by_level = $_POST['by_level'];
  
          $search_query = "SELECT * FROM donar WHERE";
          if($by_name !="") {
            $search_query .= " name='$by_name'";
          }
          if($by_sex !="") {
            $search_query .= " sex='$by_sex'";
          }
          if($by_group !="") {
            $search_query .= " blood_group='$by_group'";
          }
          if($by_level !="") {
            $search_query .= " e_level='$by_level'";
          }
          $search_query;
          $result = mysql_query($search_query);
  
          return $result;
      }
  </code></pre>
  
  <p>And here is the html</p>
  
  <pre><code>if(isset($_POST['submit'])) {
  
      $retrived_result = $donar-&gt;search_donar($_POST);
  
  }
  
     &lt;form action="" method="post"&gt;
      &lt;table width="100%" border="0" style="border:none;"&gt;
        &lt;tr&gt;
          &lt;td&gt;&lt;label&gt;Name:&amp;nbsp;&lt;/label&gt;&lt;input type="text" name="by_name" /&gt;&lt;/td&gt;
          &lt;td&gt;&lt;label&gt;Sex:&amp;nbsp;&lt;/label&gt;&lt;input type="text" name="by_sex" /&gt;&lt;/td&gt;
          &lt;td&gt;&lt;label&gt;Blood Group:&amp;nbsp;&lt;/label&gt;&lt;input type="text" name="by_group" /&gt;&lt;/td&gt;
          &lt;td&gt;&lt;label&gt;Level:&amp;nbsp;&lt;/label&gt;&lt;input type="text" name="by_level" /&gt;&lt;/td&gt;
          &lt;td&gt;&lt;input class="button" type="submit" name="submit" value="Search" /&gt;&lt;/td&gt;
        &lt;/tr&gt;
      &lt;/table&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p>Single filtering works very fine. But To filter with all I used AND , but it gives me error. Can anyone help ? </p>
  
  <p>Thanks in advance</p>