How to deal with null values submitted by multi-condition queries Table 1 contains brand, age, model board (BMW, ford, aodi) year (2013,) type (car, suv, mpv) I made three drop-down menus. the user selected query 1 and BMW in the board and 2013 in the year, but the third type did not select multi-condition query, how to handle submitted null values (conditions)
Table 1 contains brands, service life, vehicle models
Board (BMW, ford, aodi)
Year (2013)
Type (car, suv, mpv)
I have made three drop-down menus for users to select and query
1. Select 2013 in BMW and year on the board, but the third type is not selected,
However, when the type field is executed, null is sent, and the value is equal
Board = 'BMW 'AND year = "2013" AND type =''
If the field is not null, all models of BMW and 2013 are created,
Board = 'BMW 'AND year = "2013" AND type is not null
Or
Board = 'BMW 'AND year = "2013"
How to do this and share it:
------ Solution --------------------
Refer to the writing method below
$where = array();
if($board){
$where[]=" board='$board'";
}
if($year){
$where[]=" year='$year'";
}
....
$sql.=(!empry($where)) ? ' where ' . implode(' and ', $where) : '';
------ Solution --------------------
Set $ _ POST
$ _ POST = array ('board' => 'BMW ', 'year' => '000000', 'type' => '');
Then
$ W = array ();
Foreach ($ _ POST as $ k => $ v ){
If (! Empty ($ v) $ w [] = "$ k = '$ V '";
}
$ Where = join ('and', $ w );
Get
Board = 'BMW 'and year = '20140901'