简体   繁体   English

使用复选框的SQL查询

[英]SQL query using checkboxes

I've seen some questions about this topic around here, but non of them solved my problem. 我在这里看到了有关此主题的一些问题,但没有一个问题解决了我的问题。 I'm trying to make a simple filter using SQL and check boxes. 我正在尝试使用SQL和复选框创建一个简单的过滤器。

So far I got this code: 到目前为止,我得到了以下代码:

<form action="#" method="GET" id="filter"> 
    <input type="checkbox" name="omgeving[]" value="Indoor">Indoor<br /> 
    <input type="checkbox" name="omgeving[]" value="Outdoor">Outdoor<br /> 
    <input type="submit" name="kies" value="Kies" /> 
</form> 
<?php 
if(isset($_GET['omgeving']) && count($_GET['omgeving'] > 0)) { 
    $catid = implode(',', $_GET['omgeving']);
    $catid = mysql_real_escape_string($catid); 

    $query = mysql_query("SELECT * FROM SomeWhere WHERE Omgeving IN (".$catid.")"); 

    while($row = mysql_fetch_assoc($query)) { 
        $id = $row['Id']; 
        $prod = $row['Product']; 

        echo $prod . "<br />"; 
    }
}
?>

It does something when I click on submit it changes the URL to: filtertest/?omgeving[]=Indoor&kies=Kies but I think the query isn't working like it should because my footer disappears usually this means that my code is incorrect and the page stops loading further. 当我单击“ submit时,它执行了一些操作 ,将URL更改为: filtertest/?omgeving[]=Indoor&kies=Kies但我认为查询无法正常工作,因为页脚通常消失了,这意味着我的代码不正确,并且页面停止进一步加载。 For the love of me I cant figure out why this isn't working... 为了我的爱,我不知道为什么这不起作用...

Is there someone that could help me? 有没有人可以帮助我?

I've checked out this answer but this doesn't solve my problem. 我已经签出了这个答案,但这不能解决我的问题。 The code above is the code that does something at least. 上面的代码至少可以执行某些操作。

Help... what am I doing wrong here? 求助...我在这里做错了什么?

Change omgeving[] to omgeving . omgeving[]更改为omgeving so, the URL would be ?omgeving=Indoor&kies=Kies 因此,URL为?omgeving=Indoor&kies=Kies

<?php 

if(isset($_GET['omgeving']) || !empty($_GET['omgeving'])){
  $catid = mysql_real_escape_string($_GET['omgeving']); 
  //continue with what you are doing.
  // use var_dump(<variable>); exit(); to debug.
}

?>

Hope it was helpful! 希望对您有所帮助!

Change the line 换线

 $catid = implode(',', $_GET['omgeving']);

to

$catid = implode('","', $_GET['omgeving']);
$catid = '"'.$catid.'"';

Finally your query should look like below if you have multiple selected 最后,如果您选择了多个查询,则查询应如下所示

SELECT * FROM SomeWhere WHERE Omgeving IN ("Indoor","Outdoor") SELECT * FROM从何处进入(“室内”,“室外”)

$ catid = implode(“','”,$ _GET ['omgeving']);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM