2014-02-11 111 views
0

我正在使用PHP & MYSQL進行分配。使用PHP和MYSQL進行字段搜索的組合

其中一個任務是搜索任何字段的組合。這包括從數據庫填充的下拉框。和文本字段。

t2ath包含

ID 
SPORT 
COUNTRY 
GENDER 
FIRSTNAME 
LASTNAME 
Image 

我已經對這個代碼的工作了一個星期才能夠在沒有任何錯誤的任意組合進行搜索。

我想知道是否有另一種更有效的方法來做到這一點。

$selectedSport = $_POST['sport']; $gender =$_POST['gender']; $fName =$_POST['fname']; $lName =$_POST['lname']; $country =$_POST['country']; 
$sql_fName=""; $sql_lName=""; $sql_gender=""; $sql_sport=""; $sql_country=""; 
$checkFiled=False; 
$where=""; 
$and=""; 
// 
if ($selectedSport=="showAll") 
    { 
     !isset($selectedSport); 
    } 
else 
    { 
     if (isset($selectedSport)) 
      { 
       if ($checkFiled==True) 
        { 

         $sql_sport = " AND t2ath.sport = '$selectedSport'" ; 

        } 
       else 
        { 
         $sql_sport = " t2ath.sport = '$selectedSport' " ; 
         $checkFiled=True; 
        } 
      } 
     else { 
      $sql_sport = ""; 
     } 
    } 

// 
if ($country =="showAll") 
    { 
     !isset($country); 
    } 
else 
    { 
     if (isset($country)) 
      { 
       if ($checkFiled ==True) 
        { 

         $sql_country = " AND t2ath.country = '$country'" ; 

        } 
       else 
        { 
         $sql_country = " t2ath.country = '$country' " ; 
         $checkFiled=True; 
        } 
      } 
     else { 
      $sql_country = ""; 
     } 
    } 
// 
if ($gender=="Gender") 
    { 
     !isset($gender); 
    } 
else 
    { 
     if (isset($gender)) 
      { 
       if ($checkFiled ==True) 
        { 

         $sql_gender = " AND t2ath.gender = '$gender'" ; 

        } 
       else 
        { 
         $sql_gender = " t2ath.gender = '$gender' " ; 
         $checkFiled=True; 
        } 
      } 
     else { 
      $sql_gender = ""; 
     } 
    } 
// 
if ($fName =="") 
    { 
     !isset($fName); 
    } 
else 
    { 
     if (isset($fName)) 
      { 
       if ($checkFiled==True) 
        { 

         $sql_fName = " AND t2ath.firstName = '$fName'" ; 
        } 
       else 
        { 
         $sql_fName = " t2ath.firstName = '$fName' " ; 
         $checkFiled=True; 
        } 
      } 
     else { 
      $sql_fName = ""; 
     } 
    } 
// 
if ($lName =="") 
    { 
     !isset($lName); 
    } 
else 
    { 
     if (isset($lName)) 
      { 
       if ($checkFiled==True) 
        { 

         $sql_lName = " AND t2ath.lastName = '$lName' " ; 

        } 
       else 
        { 
         $sql_lName = " t2ath.lastName = '$lName' " ; 
         $checkFiled=True; 
        } 
      } 
     else 
      { 
       $sql_lName = ""; 
      } 
    } 

if ($checkFiled == True) 
    $where=" where "; 

$selectString = "SELECT t2ath.lastName,t2ath.firstName,t2ath.image,t2ath.sport,t2ath.gender,t2ath.country,t2country.flag FROM t2ath LEFT JOIN t2country 
       ON t2ath.country = t2country.name $where $sql_sport $sql_country $sql_gender $sql_fName $sql_lName "; 
$result = mysql_query($selectString); 
+1

如果您現在剛剛學習,停止使用棄用MySQL擴展和使用的mysqli(http://us1.php.net/manual/en/book.mysqli.php)擴展。除其他外,它還支持準備好的語句,這將使您的代碼更易於管理。另外代碼'!isset($ lName);'不會取消設置變量,它會返回變量是否已經設置。要取消設置變量,請使用'unset($ lName);' – Anthony

+0

爲什麼您需要取消設置這些變量?你從不在「if」塊之外引用它們。 – Barmar

+0

@Anthony實際上,像這樣動態生成的SQL是使用mysqli預處理語句的_harder_,因爲使用一組動態參數調用'mysqli_stmt_bind_params'很困難。這種類型的事情在PDO中更容易,因爲您可以使用一組參數。 – Barmar

回答

1

相反所有這些關於是否添加AND串聯到查詢時,使用數組和implode條件句。

$fields = array('sport' => 'sport', 
       'gender' => 'gender', 
       'fname' => 'firstName', 
       'lname' => 'lastName', 
       'country' => 'country'); 
$wheres = array(); 
foreach ($fields as $postfield => $dbfield) { 
    if ($_POST[$postfield] != 'showAll') { 
     $wheres[] = "$dbfield = '" . mysql_real_escape_string($_POST[$postfield]) . "'"; 
    } 
} 
$selectString = "SELECT t2ath.lastName, t2ath.firstName, t2ath.image, t2ath.sport, t2ath.gender, t2ath.country, t2country.flag 
       FROM t2ath LEFT JOIN t2country 
       ON t2ath.country = t2country.name"; 
if (count($wheres) > 0) { 
    $selectString .= " WHERE " . implode(" AND ", $wheres); 
} 
$result = mysql_query($selectString); 

要了解如何做到這一點同樣使用PDO準備語句,請參閱我的答案在這裏:What code approach would let users apply three optional variables in PHP/MySQL?

0

爲什麼不與每列評估您的字符串(這是一個指導而已,我不是建立你的PHP代碼有:

SELECT 
    * 
FROM 
    table 
WHERE 
    (ID = $id OR $id = 'showAll') 
    AND (SPORT = $sport OR $sport = 'showAll') 
    AND (COUNTRY = $country OR $country = 'showAll') 
    AND (GENDER = $gender OR $gender = 'showAll') 
    AND (FIRSTNAME = $firstname OR $firstname = 'showAll') 

只需要確保你NVL的變量的適當的值(無論是int或字符串)

+0

不應該「showAll」嗎? – Barmar

+0

此外,你缺少所有變量的引號。 – Barmar

+0

隨意編輯,因爲你認爲合適 - 這是更正確的方向比給他的代碼作業 – Trent

0

我已經做在我檢查了來自不同領域的價值,然後將它們添加到過去類似的東西一系列數組。我爲select,from,where,order創建了一個數組。您可以爲組或限制等其他設置執行類似操作。然後我運行'array_unique',將它們分解並放入SQL字符串中。

$array_select = array('users.Id'); // SET SOME DEFAULTS SO THE QUERY WILL ALWAYS RUN 
$array_from = array('users'); 
$array_where = array(); 
$array_order = array(); 

if (isset($first_name)) { 
    $array_select[] = 'First_Name'; 
    $array_from[] = 'users'; 
} 

if (isset($city)) { 
    $array_select[] = 'City'; 
    $array_from[] = 'user_contact'; 
    $array_where[] = 'users.Id = user_contact.City'; 
} 

if ($array_select) { 
    $array_select = array_unique($array_select); 
    $string_select = implode(', ', $array_select); 
} 
if ($array_where) { 
    $array_where = array_unique($array_where); 
    $string_where = 'WHERE '.implode(' AND ', $array_where); 
} 
// REPEAT FOR OTHERS ... 



// BUILD THE QUERY OUT 
$sql = 'SELECT '.$string_select.' FROM '.$string_from.' '.$string_where.' ...