【发布时间】:2015-12-17 14:26:41
【问题描述】:
我有一个包含多个输入的表单,这些输入是我的过滤器。 这是我的代码(不是全部,只是我要修复的部分):
$req_resumo = '';
$req_status = '';
$req_usuario = '';
$n_req = 0;
$parametros = "";
// Checks which fields are filled and increases the number of filters for future usage
if (isset($_POST['usuario']) && $_POST['usuario'] != "") {
$req_usuario = $_POST['usuario'];
$n_req++;
}
if (isset($_POST['resumo']) && $_POST['resumo'] != "") {
$req_resumo = $_POST['resumo'];
$n_req++;
}
if (isset($_POST['status']) && $_POST['status'] != "") {
$req_status = $_POST['status'];
$n_req++;
}
// Then (there is some code between these parts)
if ($n_req > 0 && $funcao != 'usuario') $parametros.= " where ";
if ($req_usuario != "") {
$parametros.= " usuario = '$req_usuario' ";
if ($n_req > 1) $parametros.= " and ";
}
if ($req_resumo != "") {
$parametros.= " resumo = '$req_resumo' ";
if ($n_req > 1 && ($req_status != "") || ($req_data_inicial != "")) $parametros.= " and ";
}
if ($req_status != "") {
$parametros.= " status = '$req_status' ";
}
// This will create the query and add the parameters string at the end.
$tot = mysqli_query($con, "SELECT * FROM solicitacoes $parametros");
这段代码看起来很难看,即使对我(初学者)来说,感觉也不对,听起来不像是编码方式。
那么,有没有更好更简单的方法来构建这段代码?
【问题讨论】:
-
您缺少 where 子句“SELECT * FROM solicitacoes WHERE $parametros”
-
是的,我已经有了,我忘了,对不起!我已经添加了这一行。
-
发布的答案有任何运气/问题吗?