2017-08-09 12 views
-2

私はEコマースWebアプリケーションを開発しています。これは、フィルタオプションを使用してさまざまな製品を検索しています。phpとmysqlを使用して検索とフィルタ条件を簡略化する方法

使用しているフィルタオプションはstore, price, discount, color, sizeです。

これはif elseを使用して条件を作成し、データベースで検索します。

マイコードが

if($_GET["cid"] != null or $_GET["size"] != null or $_GET["dis"] != null or $_GET["prf"] != null or $_GET["prt"] != null or $_GET["store"] != null or $_GET["cat"] != null or $_GET["sub"] != null or $_GET["brand"] != null) { 
     $query = null; 
     $query_w = null; 
     $query .= 'SELECT * FROM tbl_products a INNER JOIN tbl_product_category c ON c.product_category_id = a.product_category_id LEFT JOIN tbl_product_sec_category sc ON sc.product_sec_category_id = a.product_sec_category_id LEFT JOIN tbl_product_subcategory s ON s.product_subcategory_id = a.product_subcategory_id LEFT JOIN tbl_product_brand b ON b.product_brand_id = a.product_brand_id'; 

     if($_GET["cat"] != null) 
      $query_w .= ' a.product_sec_category_id = "'.$_GET["cat"].'"'; 
     if($_GET["sub"] != null) 
      $query_w .= ' a.product_subcategory_id = "'.$_GET["sub"].'"'; 
     if($_GET["brand"] != null) 
      $query_w .= ' a.product_brand_id = "'.$brand.'"'; 
     if($_GET["cid"] != null) { 
      $query .= ' INNER JOIN tbl_fa_api_color ac ON ac.api_product_id = a.api_product_id INNER JOIN tbl_product_color pc ON pc.product_color_id = ac.product_color_id'; 
      $query_w .= ' AND ac.product_color_id = "'.$_GET["cid"].'"'; 
     } 
     if($_GET["size"] != null) { 
      $query .= ' INNER JOIN tbl_fa_api_size ac ON ac.api_product_id = a.api_product_id'; 
      $query_w .= ' AND ac.api_size_size = "'.$_GET["size"].'"'; 
     } 
     if($_GET["dis"] != null) 
      $query_w .= ' AND (a.api_discount BETWEEN "'.$_GET["dis"].'" AND "99")'; 
     if($_GET["prf"] != null and $_GET["prt"] != null) 
      $query_w .= ' AND (a.api_retail BETWEEN "'.$_GET["prf"].'" AND "'.$_GET["prt"].'")'; 
     if($_GET["store"] != null) { 
      if($_GET["store"] == 'a') 
       $query_w .= ' AND a.api_type = "One"'; 
      else if($_GET["store"] == 'f') 
       $query_w .= ' AND a.api_type = "Two"'; 
      else if($_GET["store"] == 'af' or $_GET["store"] == 'fa') 
       $query_w .= ' AND (a.api_type = "One" OR a.api_type = "Two")'; 
     } 

     $query_w .= ' AND a.api_status = 1 ORDER BY a.api_id DESC LIMIT 0,6'; 

     echo $query. " WHERE".$query_w; 
    } 

$_GET["cid"]colorである、$_GET["dis"]$_GET["sub"]は、上記のコードでsubcategory

ある、$_GET["cat"]categoryある、$_GET["prt"]price toある、$_GET["prf"]price fromで、discountされた連結を使用していmysql queryを入手してください。しかし、私のコードは正しくありません。 mysql query is wrong

例えば、

$_GET["cid"] = 1$_GET["dis"] = 12電流mysql queryは、上記のコードが間違っている

SELECT * FROM tbl_products a INNER JOIN tbl_product_category c ON c.product_category_id = a.product_category_id LEFT JOIN tbl_product_sec_category sc ON sc.product_sec_category_id = a.product_sec_category_id LEFT JOIN tbl_product_subcategory s ON s.product_subcategory_id = a.product_subcategory_id LEFT JOIN tbl_product_brand b ON b.product_brand_id = a.product_brand_id INNER JOIN tbl_fa_api_color ac ON ac.api_product_id = a.api_product_id INNER JOIN tbl_product_color pc ON pc.product_color_id = ac.product_color_id WHERE and ac.product_color_id = "1" AND (a.api_discount BETWEEN "12" AND "99") AND a.api_status = 1 ORDER BY a.api_id DESC limit 0, 6

あります。

連結を使用して正しくmysql queryを作成する方法

私のif else conditionを簡略化する方法はありますか?ここにはまった。ありがとうございました。

+1

あなたのコードはSQLインジェクション攻撃に対して脆弱です。パラメータ化されたクエリを使用して自分自身を保護する必要があります。簡単な説明とそれを安全に行うためのPHPの例については、http://bobby-tables.com/を参照してください。文字列を連結することは安全ではありません。あなたのデータは、ユーザーからの悪質な入力によってハッキング、盗難、破損、または削除される可能性があります。 – ADyson

+0

とにかく、あなたの特定の問題に関連して、「間違った」クエリの例を教えてくれましたが、間違っていると思われる部分や、 「どこか」は明らかに間違っているように見えますか?句を追加するたびに、WHERE句をすでに追加したかどうかをチェックし、必要に応じて句の先頭にANDを書いたり書いたりしないでください。まさに$ query_wがnullかどうかを確認することができます。 – ADyson

+0

@ADyson '$ query'と' $ query_w'を修正して、すべての条件に対してcoreectのmysqlクエリを表示する必要があります。 –

答えて

2

このように条件を設定できます。しかし、このクエリをより安全に実行するには、PDOまたはmysqliにお勧めします。

if($_GET["cid"] != null or $_GET["size"] != null or $_GET["dis"] != null or $_GET["prf"] != null or $_GET["prt"] != null or $_GET["store"] != null or $_GET["cat"] != null or $_GET["sub"] != null or $_GET["brand"] != null) { 
$condition = array();$query_w = ''; 
$query = 'SELECT * FROM tbl_products a INNER JOIN tbl_product_category c ON c.product_category_id = a.product_category_id LEFT JOIN tbl_product_sec_category sc ON sc.product_sec_category_id = a.product_sec_category_id LEFT JOIN tbl_product_subcategory s ON s.product_subcategory_id = a.product_subcategory_id LEFT JOIN tbl_product_brand b ON b.product_brand_id = a.product_brand_id'; 

if($_GET["cat"] != null) 
    $condition[] = 'a.product_sec_category_id = "'.$_GET["cat"].'"'; 
if($_GET["sub"] != null) 
    $condition[] = 'a.product_subcategory_id = "'.$_GET["sub"].'"'; 
if($_GET["brand"] != null) 
    $condition[] = 'a.product_brand_id = "'.$brand.'"'; 
if($_GET["cid"] != null) { 
    $query .= ' INNER JOIN tbl_fa_api_color ac ON ac.api_product_id = a.api_product_id INNER JOIN sh17n_product_color pc ON pc.product_color_id = ac.product_color_id'; 
    $condition[] = 'ac.product_color_id = "'.$_GET["cid"].'"'; 
} 
if($_GET["size"] != null) { 
    $query .= ' INNER JOIN tbl_fa_api_size ac ON ac.api_product_id = a.api_product_id'; 
    $condition[] = 'ac.api_size_size = "'.$_GET["size"].'"'; 
} 
if($_GET["dis"] != null) 
    $condition[] = '(a.api_discount BETWEEN "'.$_GET["dis"].'" AND "99")'; 
if($_GET["prf"] != null and $_GET["prt"] != null) 
    $condition[] = '(a.api_retail BETWEEN "'.$_GET["prf"].'" AND "'.$_GET["prt"].'")'; 
if($_GET["store"] != null) { 
    if($_GET["store"] == 'a') 
     $condition[] = 'a.api_type = "One"'; 
    else if($_GET["store"] == 'f') 
     $condition[] = 'a.api_type = "Two"'; 
    else if($_GET["store"] == 'af' or $_GET["store"] == 'fa') 
     $condition[] = '(a.api_type = "One" OR a.api_type = "Two")'; 
} 

if(!empty($condition)){ 
    $query_w = implode(' AND ', $condition); 
} 

if($query_w != '') 
    $query_w .= ' AND a.api_status = 1 ORDER BY a.api_id DESC LIMIT 0,6'; 
else 
    $query_w = 'a.api_status = 1 ORDER BY a.api_id DESC LIMIT 0,6'; 

echo $query. " WHERE ".$query_w; 
} 
+1

WHERE条件がありません。 –

関連する問題