多條件查詢,提交的空值(條件)怎麼辦

來源:互聯網
上載者:User
多條件查詢,提交的空值(條件)怎麼處理
表1裡有品牌,年限,車型
board(BMW,ford,aodi)
year(2011,2012,2013)
type(car,suv,mpv)

我做了三個下拉式功能表,使用者搭配選取查詢
1,選了board裡的BMW和year裡的2013,但第三個type沒選,

但在做的時候type欄位就會發送空,查詢時就等於
board='BMW'AND year="2013" AND type=''
我要實現 不選擇的欄位IS NOT NULL就是BMW和2013的所有車型,
board='BMW'AND year="2013" AND type is not null

或者

board='BMW'AND year="2013"
這個怎麼做,

分享到:


------解決方案--------------------
可以參考一下寫法
$where = array();
if($board){
$where[]=" board='$board'";
}
if($year){
$where[]=" year='$year'";
}
....
$sql.=(!empry($where)) ? ' where ' . implode(' and ', $where) : '';

------解決方案--------------------
設提交後 $_POST 為
$_POST = array('board' => 'BMW', 'year' => '2013', 'type' => '');

$w = array();
foreach($_POST as $k=>$v) {
if(! empty($v)) $w[] = "$k='$v'";
}
$where = join(' and ', $w);
得到
board='BMW' and year='2013'
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.