简体   繁体   中英

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.

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. 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 . so, the URL would be ?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")

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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