PHP / MSSQL - 从用户输入过滤(HTML) [英] PHP/MSSQL - Filtering from User Input (HTML)

查看:176
本文介绍了PHP / MSSQL - 从用户输入过滤(HTML)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了这个任务,包括通过用户输入对我当前的SQL查询进行某种过滤。
基本上,我正在寻找一个过滤选项,无论它的某种菜单或按钮,真的没有关系。我的mssql如下所示:

$ pre $ SELECT $ WHERE Test_Database.Week ='(USER INPUT GOES HERE)'
GROUP BY Distributor
ORDER BY Value desc

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

问候

$你可以简单地通过引入if else语句
$ where =来实现这个功能。
//接收过滤器选项示例$ _GET ['week']
//为$ _GET ['week']
做一些消毒如果($ _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


I have been given this assignment, to include some sort of filtering to my current SQL query via User Input. Basically, i am looking for a filtering option, whether its some kind of menu or button, really doesn't matter. My mssql is as follows:

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

How can i make the WHERE statement a User Input? For instance.. A client wants to see the given value of some distributor, but in the week/month/year of the clients choice.

Regards

解决方案

 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";
  }

//You can add multiple condition by concatenating $where, but make sure where not repeats

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

这篇关于PHP / MSSQL - 从用户输入过滤(HTML)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆