php min和max变量在mysql语句中不起作用

时间:2015-07-16 08:29:54

标签: php jquery mysql

我正在使用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 

变量正在发布,因为我在测试页面上回显了它们。

如果我手动设置最小和最大变量,则可行。

1 个答案:

答案 0 :(得分:1)

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

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

修改

变量已发布

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

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

如果您不确定数据是已发布还是通过网址中的查询字符串发送,请使用 $_REQUEST

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