Php 使用表单选项过滤MYSQL查询

Php 使用表单选项过滤MYSQL查询,php,mysql,select,Php,Mysql,Select,我有一个带有多个输入的表单,它们是我的过滤器。 这是我的代码(不是全部,只是我想修复的部分): 这段代码看起来很难看,即使对我(begginer)来说,它也感觉不对,听起来不像是编码的方式 那么,有没有更好更简单的方法来构建此代码 $predicates = array(); if ($_POST['usuario'] != "") { $predicates[] = "usuario = '{$_POST["usuario"]}'"; } if ($_POST['resumo'] !=

我有一个带有多个输入的表单,它们是我的过滤器。 这是我的代码(不是全部,只是我想修复的部分):

这段代码看起来很难看,即使对我(begginer)来说,它也感觉不对,听起来不像是编码的方式

那么,有没有更好更简单的方法来构建此代码

$predicates = array();
if ($_POST['usuario'] != "") {
    $predicates[] = "usuario = '{$_POST["usuario"]}'";
}
if ($_POST['resumo'] != "") {
    $predicates[] = "resumo = '{$_POST["resumo"]}'"
}
if ($_POST['status'] != "") {
    $predicates[] = "status = '{$_POST["status"]}'"
}
if (count($predicates) == 0) {
    // handle case when nothing specified in POST
} else {
    $tot = mysqli_query($con, "SELECT * FROM solicitacoes WHERE "
        . implode(" and ", $predicates) );
}
我可能没有你所有的逻辑完全符合要求。。。但是想法是有的。使用
inclode()
WHERE
子句的谓词之间插入
(它将计算出需要多少个谓词,如果有的话)。此外,由于提交POST的是HTML表单,因此可以确定每个POST变量至少传递了一些值(因此不需要
isset()

我可能没有你所有的逻辑完全符合要求。。。但是想法是有的。使用
inclode()
WHERE
子句的谓词之间插入
(它将计算出需要多少个谓词,如果有的话)。此外,由于提交POST的是HTML表单,因此可以确定每个POST变量至少传递了一些值(因此不需要
isset()

我可能没有你所有的逻辑完全符合要求。。。但是想法是有的。使用
inclode()
WHERE
子句的谓词之间插入
(它将计算出需要多少个谓词,如果有的话)。此外,由于提交POST的是HTML表单,因此可以确定每个POST变量至少传递了一些值(因此不需要
isset()


我可能没有你所有的逻辑完全符合要求。。。但是想法是有的。使用
inclode()
WHERE
子句的谓词之间插入
(它将计算出需要多少个谓词,如果有的话)。此外,由于提交POST的是HTML表单,因此可以确定每个POST变量至少传递了一些值(因此不需要
isset()

试试这个。从我的本地测试(没有db)来看是正确的

$n_req = 0;
$_POST['usuario'] = 'test';
$_POST['resumo'] = 'test2';
$_POST['status'] = 'test3';
if (!empty($_POST['usuario'])) {
$req_usuario = $_POST['usuario'];
$where[] = " usuario = ? ";
$params[] = $req_usuario;
$n_req++;
}
if (!empty($_POST['resumo'])) {
$req_resumo = $_POST['resumo'];
$where[] = " resumo = ? ";
$params[] = $req_resumo;
$n_req++;
}
if (!empty($_POST['status'])) {
    $req_status = $_POST['status'];
$where[] = " status = ? ";
$params[] = $req_status;
$n_req++;
}
$sql_where = !empty($where) ? ' where ' . implode(' and ', $where) : '';
echo $sql_where;
$tot = mysqli_prepare($con, "SELECT * FROM solicitacoes $sql_where");
if(!empty($params)) {
//foreach($params as $param) {
//  mysqli_stmt_bind_param($tot, "s", $param);
    //echo $param;
//}
$params = array_merge(array($tot),
                  array(str_repeat('s', count($params))), 
                  array_values($params));
print_r($params);
call_user_func_array('mysqli_stmt_bind_param', $params);
// adapated from https://stackoverflow.com/questions/793471/use-one-bind-param-with-variable-number-of-input-vars and http://www.pontikis.net/blog/dynamically-bind_param-array-mysqli may need to be altered
}
echo "SELECT * FROM solicitacoes $sql_where";
mysqli_execute($tot);
如果这三个值都已填充,则查询应为

从usuario=?的请求中选择*?和resumo=?和状态=

将由驱动程序在稍后的过程中用值填充。这可以防止用户添加恶意代码来操纵SQLs处理


我也没有看到在哪里设置了
$funcao


您可以注释掉
mysqli
函数并去掉回显行以查看代码的作用。这就是我确认查询按预期构建的方式。

尝试一下。从我的本地测试(没有db)来看是正确的

$n_req = 0;
$_POST['usuario'] = 'test';
$_POST['resumo'] = 'test2';
$_POST['status'] = 'test3';
if (!empty($_POST['usuario'])) {
$req_usuario = $_POST['usuario'];
$where[] = " usuario = ? ";
$params[] = $req_usuario;
$n_req++;
}
if (!empty($_POST['resumo'])) {
$req_resumo = $_POST['resumo'];
$where[] = " resumo = ? ";
$params[] = $req_resumo;
$n_req++;
}
if (!empty($_POST['status'])) {
    $req_status = $_POST['status'];
$where[] = " status = ? ";
$params[] = $req_status;
$n_req++;
}
$sql_where = !empty($where) ? ' where ' . implode(' and ', $where) : '';
echo $sql_where;
$tot = mysqli_prepare($con, "SELECT * FROM solicitacoes $sql_where");
if(!empty($params)) {
//foreach($params as $param) {
//  mysqli_stmt_bind_param($tot, "s", $param);
    //echo $param;
//}
$params = array_merge(array($tot),
                  array(str_repeat('s', count($params))), 
                  array_values($params));
print_r($params);
call_user_func_array('mysqli_stmt_bind_param', $params);
// adapated from https://stackoverflow.com/questions/793471/use-one-bind-param-with-variable-number-of-input-vars and http://www.pontikis.net/blog/dynamically-bind_param-array-mysqli may need to be altered
}
echo "SELECT * FROM solicitacoes $sql_where";
mysqli_execute($tot);
如果这三个值都已填充,则查询应为

从usuario=?的请求中选择*?和resumo=?和状态=

将由驱动程序在稍后的过程中用值填充。这可以防止用户添加恶意代码来操纵SQLs处理


我也没有看到在哪里设置了
$funcao


您可以注释掉
mysqli
函数并去掉回显行以查看代码的作用。这就是我确认查询按预期构建的方式。

尝试一下。从我的本地测试(没有db)来看是正确的

$n_req = 0;
$_POST['usuario'] = 'test';
$_POST['resumo'] = 'test2';
$_POST['status'] = 'test3';
if (!empty($_POST['usuario'])) {
$req_usuario = $_POST['usuario'];
$where[] = " usuario = ? ";
$params[] = $req_usuario;
$n_req++;
}
if (!empty($_POST['resumo'])) {
$req_resumo = $_POST['resumo'];
$where[] = " resumo = ? ";
$params[] = $req_resumo;
$n_req++;
}
if (!empty($_POST['status'])) {
    $req_status = $_POST['status'];
$where[] = " status = ? ";
$params[] = $req_status;
$n_req++;
}
$sql_where = !empty($where) ? ' where ' . implode(' and ', $where) : '';
echo $sql_where;
$tot = mysqli_prepare($con, "SELECT * FROM solicitacoes $sql_where");
if(!empty($params)) {
//foreach($params as $param) {
//  mysqli_stmt_bind_param($tot, "s", $param);
    //echo $param;
//}
$params = array_merge(array($tot),
                  array(str_repeat('s', count($params))), 
                  array_values($params));
print_r($params);
call_user_func_array('mysqli_stmt_bind_param', $params);
// adapated from https://stackoverflow.com/questions/793471/use-one-bind-param-with-variable-number-of-input-vars and http://www.pontikis.net/blog/dynamically-bind_param-array-mysqli may need to be altered
}
echo "SELECT * FROM solicitacoes $sql_where";
mysqli_execute($tot);
如果这三个值都已填充,则查询应为

从usuario=?的请求中选择*?和resumo=?和状态=

将由驱动程序在稍后的过程中用值填充。这可以防止用户添加恶意代码来操纵SQLs处理


我也没有看到在哪里设置了
$funcao


您可以注释掉
mysqli
函数并去掉回显行以查看代码的作用。这就是我确认查询按预期构建的方式。

尝试一下。从我的本地测试(没有db)来看是正确的

$n_req = 0;
$_POST['usuario'] = 'test';
$_POST['resumo'] = 'test2';
$_POST['status'] = 'test3';
if (!empty($_POST['usuario'])) {
$req_usuario = $_POST['usuario'];
$where[] = " usuario = ? ";
$params[] = $req_usuario;
$n_req++;
}
if (!empty($_POST['resumo'])) {
$req_resumo = $_POST['resumo'];
$where[] = " resumo = ? ";
$params[] = $req_resumo;
$n_req++;
}
if (!empty($_POST['status'])) {
    $req_status = $_POST['status'];
$where[] = " status = ? ";
$params[] = $req_status;
$n_req++;
}
$sql_where = !empty($where) ? ' where ' . implode(' and ', $where) : '';
echo $sql_where;
$tot = mysqli_prepare($con, "SELECT * FROM solicitacoes $sql_where");
if(!empty($params)) {
//foreach($params as $param) {
//  mysqli_stmt_bind_param($tot, "s", $param);
    //echo $param;
//}
$params = array_merge(array($tot),
                  array(str_repeat('s', count($params))), 
                  array_values($params));
print_r($params);
call_user_func_array('mysqli_stmt_bind_param', $params);
// adapated from https://stackoverflow.com/questions/793471/use-one-bind-param-with-variable-number-of-input-vars and http://www.pontikis.net/blog/dynamically-bind_param-array-mysqli may need to be altered
}
echo "SELECT * FROM solicitacoes $sql_where";
mysqli_execute($tot);
如果这三个值都已填充,则查询应为

从usuario=?的请求中选择*?和resumo=?和状态=

将由驱动程序在稍后的过程中用值填充。这可以防止用户添加恶意代码来操纵SQLs处理


我也没有看到在哪里设置了
$funcao


您可以注释掉
mysqli
函数并去掉回显行以查看代码的作用。这就是我确认查询按预期生成的方式。

您缺少where子句,“SELECT*FROM QUOTACOES where$parametros”是的,我已经有了,我忘了,对不起!我已经添加了这一行。是否有任何关于已发布答案的运气/问题?您缺少where子句,“SELECT*FROM QUOTACOES where$parametros”是的,我已经有了,我忘了,对不起!我已经添加了这一行。是否有任何关于已发布答案的运气/问题?您缺少where子句,“SELECT*FROM QUOTACOES where$parametros”是的,我已经有了,我忘了,对不起!我已经添加了这一行。是否有任何关于已发布答案的运气/问题?您缺少where子句,“SELECT*FROM QUOTACOES where$parametros”是的,我已经有了,我忘了,对不起!我已经添加了这一行。如果没有设置
$\u POST
,这将抛出警告。索引将是未定义的。您需要
isset
s或
empty
s。如果您不熟悉SQL注入,您还应该仔细阅读。这个答案对于SQL注入是完全开放的(就像OPs代码一样)。如果没有设置
$\u POST
,这将抛出警告。索引将是未定义的。您需要
isset
s或
empty
s。如果您不熟悉SQL注入,您还应该仔细阅读。这个answ