php - Construct AND query if parameter is empty -


i'm allowing user search 10 different criteria. i'm picking url. problem if of criteria is/are empty. search record in database empty record.

$pretrazi_drzava=quote_smart($_get["pretrazi_drzava"]); $pretrazi_broj=quote_smart($_get["pretrazi_broj"]); $pretrazi_spol=quote_smart($_get["pretrazi_spol"]); $pretrazi_boja=quote_smart($_get["pretrazi_boja"]); $pretrazi_rasa=quote_smart($_get["pretrazi_rasa"]); $pretrazi_ime=quote_smart($_get["pretrazi_ime"]); $pretrazi_godina=quote_smart($_get["pretrazi_godina"]); $pretrazi_status=quote_smart($_get["pretrazi_status"]); $pretrazi_otac=quote_smart($_get["pretrazi_otac"]); $pretrazi_majka=quote_smart($_get["pretrazi_majka"]); 

it easy or

...and (mg_drzava.drzava='$pretrazi_drzava'                          or mg_golub.brojgoluba='$pretrazi_broj'                          or mg_golub.spol='$pretrazi_spol'                          or mg_golub.boja='$pretrazi_boja'                          or mg_golub.rasa='$pretrazi_rasa'                         or mg_golub.ime='$pretrazi_ime'                          or mg_golub.godina='$pretrazi_godina'                         or mg_status.status='$pretrazi_status'                         or o.brojgoluba='$pretrazi_otac'                         or m.brojgoluba='$pretrazi_majka')... 

but if instead or u put , and if example mg_golub.brojgolub='' empty, search every record in database brojgolub empty , won't find nothing , won't show nothing result.

how solve situation?

you use foreach , iterate $_get array construct sql statement

$sql = "select * table";  if(count($_get) > 0) {     // append clause     $sql .= " ";      // construct     foreach($_get $key => $value)     {         if($value != '')         {             $sql .= ' '.$key.' = '.$value.' , ';         }     }       // remove last "and"     $sql = trim( $sql, 'and ');   } 

i didn't test code, reference how ignore emtpy value in sql statement. hope help.

updated: avoid 1=1 in query


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -