vendredi 28 juillet 2017

Multiple checkbox search query fetching data from database in php

I have a multiple checkbox search query which fetches data from database. Below is the code:

HTML code:

<form action="search.php" method="post">
<input type="checkbox" name="cloth_color[]" value="Red" /> Red <br>
<input type="checkbox" name="cloth_color[]" value="Yellow" /> Yellow <br>
<input type="checkbox" name="cloth_color[]" value="Blue" /> Blue <br>
<input type="checkbox" name="cloth_color[]" value="Green" /> Green <br>
<input type="checkbox" name="cloth_color[]" value="Magenta" /> Magenta <br>
<input type="checkbox" name="cloth_color[]" value="Black" /> Black <br>
<input type="checkbox" name="cloth_color[]" value="White" /> White <br>
<input type="submit" value="SEARCH">
</form>

PHP code:

<?php
$checkbox1 = $_POST['cloth_color'];  
$chk="";  
foreach($checkbox1 as $chk1)  
   {  
      $chk .= $chk1;
   }

if($_POST['cloth_color'] != "") {
$query = "SELECT * FROM clothings WHERE colorofcloth = '$chk'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
$colorofcloth = $row['colorofcloth'];

    echo 'The cloth with ' . $colorofcloth . ' color';
    echo '<br>';

}
}
?>

Now if I choose one option from the search select box I get query. But if I select two or more color I dont get the query. A help will be really appreciated.

P.S. I do have multiple joins in Mysql query but this is the place I am stuck so presenting as clear question as possible here. Also I intent to convert mysql to mysqli before the launch of this code. Thank you :)




Aucun commentaire:

Enregistrer un commentaire