2017-08-09 117 views
-2

我正在开发一个电子商务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"]discount$_GET["prf"]price from$_GET["prt"]price to$_GET["cat"]category$_GET["sub"]subcategory

在上面的代码正在使用级联到得到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注入攻击。您应该使用参数化查询来保护自己。请参阅http://bobby-tables.com/以获得简单的解释以及一些如何安全地使用PHP的示例。连接字符串,因为你有在那里是不安全的。您的数据可能会被黑客攻击,盗用,损坏或被用户恶意输入删除。 – ADyson

+0

无论如何,关于你的具体问题,你给了我们一个“不正确”查询的例子,但没有解释你认为哪个部分是错的,或者你预期查询的样子是什么? “在哪里”看起来显然是错误的,是吗?每次添加子句时,都需要检查是否已经添加了任何WHERE子句,并在适当的时候在子句的开头写入/不写入AND。很简单,你可以检查$ query_w是否为空。 – ADyson

+0

@ADyson'$ query'和'$ query_w'应该被修正以显示所有条件的coreect'mysql query'。 –

回答

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条件缺失。 –