What if a null value (condition) is submitted for a multi-condition query?

Source: Internet
Author: User
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'

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.