简体   繁体   English

PHP过滤器合并为一个SQL查询

[英]PHP filters combining into one SQL query

I'm trying to filter through my database according to filters done by visitors. 我正在尝试根据访问者完成的过滤器对数据库进行过滤。

$query = "select * from Sheet1 where";

//filter query start 
if (!empty($brand)) {
  $branddata = implode("','", $brand);
  //testing removing query
  $query .= " Brand in('$branddata') and";
}

if (!empty($model)) {
  $modeldata = implode("','", $model);
  //testing removing query
  $query .= " Model in('$modeldata') and";
}

/* if(!empty($model) && empty($brand)){

} */

if (!empty($spower) && !empty($epower)) {
  $query .= " Power>='$spower' and Power<='$epower' and";
}

if (!empty($sprice) && !empty($eprice)) {
  $query .= " Doors>='$sprice' and Doors<='$eprice'";
}

$rs = mysqli_query($conn, $query) or die("Error : " . mysqli_error($conn));

The result I wish to get is a sql query that works and has correct syntax. 我希望得到的结果是一个有效且语法正确的sql查询。 Such as select * from Sheet1 where Doors>='$sprice' and Doors<='$eprice' , if the visitor is filtering by price. 例如,如果访客按价格进行过滤, select * from Sheet1 where Doors>='$sprice' and Doors<='$eprice'

Currently, my code is made so that it simply adds a certain string to the variable. 目前,我的代码已经制作完成,因此只需将某个字符串添加到变量中即可。 This means that if you don't filter by model, it skips model, because the model variable is empty. 这意味着,如果您不按模型过滤,则会跳过模型,因为model变量为空。 The problem comes to if you filter by power, the SQL will become select * from Sheet1 where Power>='$spower' and Power<='$epower' and . 问题在于,如果您按幂进行过滤,则SQL select * from Sheet1 where Power>='$spower' and Power<='$epower' and变为select * from Sheet1 where Power>='$spower' and Power<='$epower' and Obviously this doesn't work, so I need help in making the code make sure it works for every combination of filters. 显然这是行不通的,因此我需要帮助使代码确保其适用于每种过滤器组合。

I would do it this way 我会这样

$filters=array();

if(!empty($brand)){
          $branddata =implode("','",$brand);
          //testing removing query
          $filters[]= " Brand in('$branddata')";
      }

if(!empty($model)){
          $modeldata =implode("','",$model);
          //testing removing query
          $filters[]= " Model in('$modeldata') and"; 
      }


  if(!empty($spower) && !empty($epower)){
          $filters[]= " Power>='$spower' and Power<='$epower' and"; 
      }

  if(!empty($sprice) && !empty($eprice)){
          $filters[]= " Doors>='$sprice' and Doors<='$eprice'"; 
      }

$query = "select * from Sheet1 where";  

foreach ($filters as $filter) {
$query.=' AND '.$filter;

}

 $rs = mysqli_query($conn,$query) or die("Error : ".mysqli_error($conn));

Append $query .= " 1 = 1"; 追加$query .= " 1 = 1"; at the end. 在末尾。 I did some modification in your given code. 我在您给定的代码中做了一些修改。 Have a look. 看一看。

<?php
$query = "SELECT * FROM `Sheet1` WHERE"; 

//filter query start 
if(!empty($brand)){
  $branddata = implode("','",$brand);
  $query  .= " (Brand in('$branddata')) AND";
}

if(!empty($model)){
  $modeldata = implode("','",$model);
  $query  .= " (Model in('$modeldata')) AND"; 
}

if(!empty($spower) && !empty($epower)){
  $query  .= " (Power>='$spower' AND Power<='$epower') AND"; 
}

if(!empty($sprice) && !empty($eprice)){
  $query  .= " (Doors>='$sprice' AND Doors<='$eprice') AND"; //Added 'AND'
}

$query .= " 1 = 1"; //Added new line

$rs = mysqli_query($conn,$query) or die("Error : ".mysqli_error($conn));
?>

Add AND on each query appended in if conditions. 在条件中附加的每个查询上添加AND Then, at last add $query .= " 1 = 1"; 然后,最后添加$query .= " 1 = 1"; . Which will save you from extra AND coming at the end. 这将节省您多余的AND费用。 If none of the conditions satisfy, then your query will be SELECT * FROM Sheet1 WHERE 1 = 1 . 如果没有一个条件满足,那么您的查询将是SELECT * FROM Sheet1 WHERE 1 = 1 Simple. 简单。 And, don't forget to differentiate between conditions in query. 并且,不要忘记区分查询条件。 Differentiate your conditions like how I did by opening and closing brackets. 像打开和关闭方括号一样区分您的情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM