Multi-criteria query, what should I do if I submit a null value (condition)

Source: Internet
Author: User
Multi-criteria query, the null value submitted (condition) how to handle
Table 1 has brands, years, models
Board (Bmw,ford,aodi)
Year (2011,2012,2013)
Type (CAR,SUV,MPV)

I made three drop-down menus, and the user selects the query
1, selected BMW in board and 2013 in year, but the third type was not chosen,

But when you do, the Type field is sent empty, and the query is equal to
Board= ' BMW ' and year= ' and type= '
I want to implement the not selected fields are NOT NULL are all models of BMW and 2013,
Board= ' BMW ' and year= "+" and type is not NULL

Or

Board= ' BMW ' and year= "2013"
How to do this,

Share to:


------Solution--------------------
You can refer to the wording
$where = Array ();
if ($board) {
$where []= "board= ' $board '";
}
if ($year) {
$where []= "year= ' $year '";
}
....
$sql. = (!empry ($where))? ' WHERE '. Implode (' and ', $where): ';

------Solution--------------------
After submission, $_post is
$_post = Array (' board ' = ' BMW ', ' year ' = ' + ', ' type ' = ' = ');
The
$w = Array ();
foreach ($_post as $k = = $v) {
if (! empty ($v)) $w [] = "$k = ' $v '";
}
$where = Join (' and ', $w);
Get
Board= ' BMW ' and year= ' 2013 '
  • 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.