2013-12-16 111 views
0

我在創建功能的搜索表單時遇到了一些問題。我基本上想要一個表單(在任何頁面上)去這個頁面,然後列出我的數據庫中的相關行。我的問題是,該表單同時具有文本字段和選擇字段(用於名稱和類別),並且我無法創建使這兩個值一起搜索數據庫的功能。具有多個值的搜索表單查詢 - PHP/MYSQL

所以繼承人我想要發生的事情:當你只輸入名稱而不是類別時,它將顯示從名稱,反之亦然類別和無名稱;那麼當兩者同時它只顯示與兩者行在

繼承人是我到目前爲止有:。

// 2. Create variables to store values 
if(!$_GET['search-category'] == "") { 
    $searchName = $_GET['search-name']; 
} 

if(!$_GET['search-category'] == "select-your-category") { 
    $searchCat = $_GET['search-category']; 
} 



// 2. Create the query for the stored value. Matching it against the name, summary and sub type of my item. 
$mainSearch = "SELECT attraction.*, type.type_name, sub_type.sub_type_name "; 
$mainSearch .= "FROM attraction "; 
$mainSearch .= "INNER JOIN sub_type ON attraction.sub_type = sub_type.sub_type_id "; 
$mainSearch .= "INNER JOIN type ON attraction.type = type.type_id "; 
$mainSearch .= "WHERE attraction.name LIKE '%" . $searchName . "%' AND (sub_type.sub_type_name LIKE '%" . $searchCat . "%')"; 
$mainSearch .= "ORDER BY sub_type_name ASC"; 

// 2. run query 
$result2 = $con->query($mainSearch); 
if (!$result2) { 
    die('Query error: ' . mysqli_error($result2)); 
} 

回答

0

你可以只檢查相關的值不爲空:

// 2. Create the query for the stored value. 
// Matching it against the name, summary and sub type of my item. 
$mainSearch = "SELECT attraction.*, type.type_name, sub_type.sub_type_name "; 
$mainSearch .= "FROM attraction "; 
$mainSearch .= "INNER JOIN sub_type ON attraction.sub_type = sub_type.sub_type_id "; 
$mainSearch .= "INNER JOIN type ON attraction.type = type.type_id "; 
$mainSearch .= "WHERE "; 
if ($searchName) { 
    $mainSearch .= "attraction.name LIKE '%" . $searchName . "%'"; 
    if ($searchCat) { 
     $mainSearch .= " AND "; 
    } 
} 
if ($searchCat) { 
    $mainSearch .= "sub_type.sub_type_name LIKE '%" . $searchCat . "%'" 
} 

$mainSearch .= "ORDER BY sub_type_name ASC"; 

// Double check that at least one of the search criteria is filled: 
if (!$searchName && !$searchCat) { 
    die("Must supply either name search or category search"); 
} 
+0

你好,這已經進入我的代碼,它似乎工作。但是,仍然存在這樣的問題,即當我沒有填寫輸入/選擇元素中的一個或兩個時,我現在會遇到錯誤。我如何創建一個錯誤消息來停止正在提交的表單,或者告訴網站如果它們是空白的,如果它們是空的,那麼它們不會包含在它們中,即''search-name =&search-category =' –

+0

我修改了我的答案以添加此安全檢查 – Mureinik

0

您可以做的是聲明一個名爲$ search_condition的變量,並基於$ searchName或$ searchCat是否爲null或不將值賦給$ search_condition

對於eg

if (isset($searchName) || !is_empty($searchName)) 
{ 
    $search_condition = "WHERE attraction.name LIKE '%" . $searchName; 
} 
if (isset($searchCat) || !is_empty($searchCat)) 
{ 
    $search_condition = "sub_type.sub_type_name LIKE '%" . $searchCat . "%'"; 
} 
if ((isset($searchName) || !is_empty($searchName)) && (isset($searchCat) || !is_empty($searchCat))) 
{ 
    $search_condition = "WHERE attraction.name LIKE '%" . $searchName . "%' AND (sub_type.sub_type_name LIKE '%" . $searchCat . "%')"; 
} 

希望這可以幫助你

感謝

2

我的代碼重構爲類似 -

foreach($_GET['filters'] as $fname => $fval) { 

    if(!$fval) continue; 

    $where[] = "$fname LIKE '%{$fval}%'"; 
} 

您需要包括只有那些非空的輸入在查詢中。你還需要解決安全問題就像逃脫的投入等

+0

您好,我不完全明白這是如何工作的。你能解釋我如何重構我的代碼以使其工作? –

0

這是一個評論,但我想利用格式化選項優勢...

你知道,你可以重寫是這樣。 ..

// 2. Create the query for the stored value. Matching it against the name, summary and sub type of my item. 

$mainSearch = " 
SELECT a.* 
    , t.type_name 
    , s.sub_type_name 
    FROM attraction a 
    JOIN sub_type s 
    ON a.sub_type = s.sub_type_id 
    JOIN type t 
    ON a.type = t.type_id 
WHERE a.name LIKE '%$searchName%' 
    AND s.sub_type_name LIKE '%$searchCat%' 
ORDER 
    BY s.sub_type_name ASC; 
    ";