2016-02-14 22 views
-1

我有6 <select>框。我正在根據<select>框進行報告。我所做的就是創建一個表單並將其設置爲GET,然後通過下一頁的$ _GET方法獲取這些值。像如何使PHP中的下拉式基本MySQL查詢

<form method="get" action="report.php"> 

<select name="select1"> 
<option value="none">Select value</option> 
<option value="1">1</option> 
<option value="2">2</option> 
<option value="3">3</option> 
</select> 


<select name="select2"> 
<option value="none">Select value</option> 
<option value="1">1</option> 
<option value="2">2</option> 
<option value="3">3</option> 
</select> 

<select name="select3"> 
<option value="none">Select value</option> 
<option value="1">1</option> 
<option value="2">2</option> 
<option value="3">3</option> 
</select> 
..................... 
</form> 

這裏是PHP代碼。 (這些都是樣本數據:P)試圖告訴你人們我的問題。我得到,如果它的價值等於沒有或沒有做這一切值的檢查等

if(
$cby == "none" 
&& $crequirment == "none" 
&& $cdepartment == "none" 
&& $period == "none" 
&& $ccontact == "none" 
&& $cattend == "none" 
) 
{ 
    $query = mysqli_query($db, "SELECT * FROM customer"); 
} 



else if (
$cby != "none" 
&& $crequirment == "none" 
&& $cdepartment == "none" 
&& $period == "none" 
&& $ccontact == "none" 
&& $cattend == "none" 
) 
{ 
    $query = mysqli_query($db, "SELECT * FROM customer WHERE cby = $cby"); 
} 
else if (
$cby == "none" 
&& $crequirment == "none" 
&& $cdepartment != "none" 
&& $period == "none" 
&& $ccontact == "none" 
&& $cattend == "none") 
{ 
    $query = mysqli_query($db, "SELECT * FROM customer WHERE cdepartment = $cdepartment"); 
} 
else if.......... 

我想是使盡可能簡單。我應該採用什麼方法來採用它?

+0

您的問題不明確。你需要哪些幫助? – sanders

回答

1

優化您的代碼。

使用data[cby],data[crequirment]等名稱創建選擇。
而不是none使用空字符串。

在PHP:

if (isset($_GET['data']) { 
    $where = []; 

    foreach ($_GET['data'] as $name => $value) { 
     if ($value != '') { 
      $where[] = $name."=".$value; // sanitize `$name` and `$value` first 
     } 
    } 

    if (!empty($where)) { 
     $whereString = ' WHERE '.implode(' AND ', $where); 
    } else { 
     $whereString = ''; 
    } 

    $query = mysqli_query($db, "SELECT * FROM customer".$whereString); 
}