Search Filtering with PHP/MySQL

后端 未结 7 1928
执念已碎
执念已碎 2021-01-30 12:08

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

7条回答
  •  一个人的身影
    2021-01-30 12:18

    There in Your code there is problem in query where condition . Here your query will be like select * from donar where by_name = "A" by_group = "N" there is No And/Or to make where condition properly. Please try code like given below.

    $search_query = "SELECT * FROM donar";
    $query_cond = "";
    
    if($by_name !="") {
          $query_cond .= " name='$by_name'";
    }
    if($by_sex !="") {
    
          if(!empty($query_cond)){
              $query_cond .= " AND ";
           }
    
          $query_cond .= " sex='$by_sex'";
    }
    
    if($by_group !="") {
    
          if(!empty($query_cond)){
              $query_cond .= " AND ";
           }
    
          $query_cond .= " blood_group='$by_group'";
    }
    
    if($by_level !="") {
    
          if(!empty($query_cond)){
              $query_cond .= " OR ";
           }
    
          $query_cond .= " e_level='$by_level'";
     }
    
     if(!empty($query_cond)){
          $query_cond = " Where ".$query_cond;
          $search_query.$query_cond;
     }
    

    Here in code First we take $query_cond variable empty and make condition according code. and manage AND operator according that. And in last if We found $query_cond not empty then add it to $select_query.

    I hope it will be helpful for you.

    thanks

提交回复
热议问题