PHP的最小和最大变量在mysql语句中不工作


php min and max variables not working in mysql statement

我使用jQuery滑块。我使用以下代码将变量发布到我的php表单。

变量被张贴,我使用

$mincost = $_get['mincost']; and $maxcost = $_get['maxcost'];

来获取这些变量。

当我通过mysql运行这个时,我看到mysql错误,我尝试:

`price` BETWEEN '$mincost' AND '$maxcost'

显示没有结果也没有错误

也尝试:

`price` BETWEEN ".$mincost." AND ".$maxcost."

:

`price` BETWEEN $mincost AND $maxcost

这两个都显示:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 

变量被张贴,因为我在测试页面上响应了它们。

如果我手动设置min和max变量,这是有效的

您应该使用 $_GET 来检索URL参数,而不是$_get。另外,将字符串转换为integer

$mincost = (int) $_GET['mincost'];
$maxcost = (int) $_GET['maxcost']; 

编辑

变量被张贴

如果数据为post格式,则应使用$_POST格式。

$mincost = (int) $_POST['mincost'];
$maxcost = (int) $_POST['maxcost'];

如果您不确定数据是 post 还是在URL中的querystring中发送,请使用 $_REQUEST

$mincost = (int) $_REQUEST['mincost'];
$maxcost = (int) $_REQUEST['maxcost'];