簡體   English   中英

PHP / MySQL中的可選變量下拉搜索

[英]Optional Variables in PHP/MySQL Drop down Search

我在這里很難找到這個問題的明確答案...我試圖弄清楚如何使用可選變量過濾MySQL查詢。 例如,我希望人們能夠從下拉菜單中選擇月份,年份或狀態...,或者他們只能選擇其中的兩個或全部。

另外,可以使用$ _GET完成此操作,以便在用戶提交請求時,URL類似於http://myurl.com/triathlon/?state=CA&month=January嗎?

您可以在以下頁面的頂部看到使用此示例的示例: http : //legfly.com/triathlon

非常感謝,我希望這個問題的撰寫方式有意義。 再次感謝。

你的PHP將是這樣的:

$query = "SELECT * FROM my_table";

$where_clause = array() ;
if($_GET['month'] != null){
    $where_clause[] = "`perma_month` = '$_GET[month]'";
}
if($_GET['year'] != null){
    $where_clause[] = "`perma_year` = '$_GET[year]'";
}
if($_GET['state'] != null){
    $where_clause[] = "`state` = '$_GET[state]'";
}
if(count($where_clause))
    $query .= " WHERE " . implode(' AND ' , $where_clause);

 $result = mysql_query($query) ;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM