Search Filtering with PHP/MySQL

后端 未结 7 1912
执念已碎
执念已碎 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:27

    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";
    
        $and = '';
        if(count($_POST) > 1) {
            $and ='AND';
        }
        if($_POST[0]) {
            $and ='';
        }
    
        if($by_name !="") {
            $search_query .= $and." name='$by_name'";
        }
        if($by_sex !="") {
            $search_query .= $and." sex='$by_sex'";
        }
        if($by_group !="") {
            $search_query .= $and." blood_group='$by_group'";
        }
        if($by_level !="") {
            $search_query .= $and." e_level='$by_level'";
        }
    
        if(count($_POST) == 0) {
            $search_query .= " 1 ";
        }
    
        $search_query;
        $result = mysql_query($search_query);
    
        return $result;
    }
    

提交回复
热议问题