小编典典

PHP / MSSQL-从用户输入(HTML)进行过滤

sql

我已获得此分配,以通过用户输入对当前的SQL查询进行某种过滤。基本上,我正在寻找筛选选项,无论是某种菜单还是按钮,都没有关系。我的mssql如下:

SELECT TOP 10 Test_Database.Distributor, Test_Database.Value
    FROM Test_Database
    WHERE Test_Database.Week = '(USER INPUT GOES HERE)'
    GROUP BY Distributor 
    ORDER BY Value desc

我如何使该WHERE语句成为用户输入?例如,客户希望看到某个分销商的给定价值,但要查看客户选择的周/月/年。

问候


阅读 172

收藏
2021-04-15

共1个答案

小编典典

 You can do that simply by introducing if else statement
 $where  = "";
 //receive filter option  example $_GET['week']  
  //Do some sanitizing for $_GET['week']
  if ($_GET['week']) {
  $where  =  "WHERE Test_Database.Week = $_GET['week']"
 } else if (somecondition) {
  $where  = "some query";
  }

//您可以通过串联$ where来添加多个条件,但要确保在哪里不重复

$query = "SELECT TOP 10 Test_Database.Distributor, Test_Database.Value
FROM Test_Database
$where  
GROUP BY Distributor
ORDER BY Value desc "
2021-04-15