假设我在表格中有以下信息。 -1表示无限
Price Minutes SMS Data
7.50 200 -1 250
10 250 -1 1000
12 250 -1 -1
在我的表单上,我有很多字段和一个复选框,如果你想要无限的东西,所以我有一个SQL查询,它有一些变量取决于复选框是否勾选如下:
//check to see if minutes is set as unlimited. if they are, set sms as -1
if ($minunlim=="on"){
$minquery = "minutes = -1";
$minutes = "Unlimited";
} else {
$minutes = $_GET["minutes"];
$minquery = "minutes >= '$minutes' OR minutes = -1";
}
//check to see if sms is set as unlimited. if they are, set sms as -1
if ($smsunlim=="on"){
$smsquery = "sms = -1";
$sms = "Unlimited";
} else {
$sms = $_GET["sms"];
$smsquery = "sms >= '$sms' OR sms = -1";
}
//check to see if sms is set as unlimited. if they are, set sms as -1
if ($dataunlim=="on"){
$dataquery = "data = -1";
$data = "Unlimited";
} else {
$data = $_GET["data"];
$dataquery = "data >= '$data' OR data = -1";
}
//run query
$getprices = $this->db->query("SELECT * FROM gg_Crates WHERE
price <= '$price' AND $minquery
AND ($smsquery)
AND ($dataquery)");
所以我使用的标准是Price = 36,Minutes = 100,SMS = 200,Data = 300,我的期望是在这种情况下,返回的唯一项目是行的价格为10和12(因为数据在第一行低于300)
它输出的查询是:
SELECT * FROM gg_Crates WHERE price <= '36' AND minutes >= '100' OR minutes = -1 AND (sms >= '200' OR sms = -1) AND (data >= '300' OR data = -1)
在我看来,这是
选择价格低于36,分钟更高或更高的所有商品 等于100或等于-1。并且SMS大于或等于200或 等于-1。并且数据大于或等于300或等于-1
由于数据低于250,因此该语句中没有位置应该拉出7.50行。
然而,我得到的实际结果是它返回所有3行。有什么建议吗?
答案 0 :(得分:1)
你的逻辑可能有问题,
此
$smsquery = "sms >= '$sms' OR sms = -1"
和此:
data >= '$data' OR data = -1
是一个有问题的表示,将您的查询更改为:
$getprices = $this->db->query("SELECT * FROM gg_Crates WHERE
price <= '$price' AND ($minquery)
AND ($smsquery)
AND ($dataquery)");
它会解决它
编辑K20:$ minquery需要在括号中