mardi 29 novembre 2016

HOw to make mysql query based on multiple checkboxes

I am trying to make a query based on multiple checkboxes.(For exapmle for e-shop )

Checkboxes

         <div id="car">       
            <label><input type="checkbox" value="red" >red</label>
            <label><input type="checkbox" value="black" >black</label>
            <label><input type="checkbox" value="blue" >blue</label>          
          </div>


        <div id="brand">
            <label><input type="checkbox" value="mazda" >mazda</label>
            <label><input type="checkbox" value="bmw" >bmw</label>

        </div>

Query

 $brand = $_POST['brand'];
 $color = $_POST['color'];

$Query = "SELECT * FROM items WHERE brand IN ('".implode(",", $brand)."') ";

$QueryResult = mysqli_query($connection , $Query);


while($QueryRow = mysqli_fetch_assoc($QueryResult)){
?>
<div><p><?php echo $QueryRow['cost'];?></p></div>
<div><img src="<?php echo $QueryRow['img']; ?>"></div>
<?php
}
}
    ?>

Also , I have js script for passing array with values taken from checkboxes, but it doesn't related to problem, that is why I do not post script.

Problem:

1- How can I add AND in query for taking into account color to?

2- How to avoid error if COLOR-check box have not been checked?

Any ideas?




Aucun commentaire:

Enregistrer un commentaire