jeudi 8 janvier 2015

multiple checkboxes used for search in php and mysql?

This seems to be a common question as I have seen plenty of similar questions.


however, none of the answers actually pointing out how to do the selecting from mysql database and this is my issue as the moment.


basically I have a table which I store the search data in it.


it looks like this:



id blond darkHair busty curvy
---------------------------------------------------

1 blond busty

2 dark hair busty curvy

3 blond curvy

4 blond curvy


and I have a form with checkboxes like so:



<form action="search.php" method="post">
<input name="keyword[]" type="checkbox" value="blond" />
<input name="keyword[]" type="checkbox" value="dark hair" />
<input name="keyword[]" type="checkbox" value="busty" />
<input name="keyword[]" type="checkbox" value="curvy" />
</form>


and the PHP codes like this:



if(isset($_POST['keyword']))
{

$keyword = $_POST['keyword'];

foreach ($_POST['keyword'] as $keyword) {

$keywordarray[] = mysqli_real_escape_string($conx, $keyword);


}
$keywords = implode (",", $keywordarray);

$sql = "SELECT * FROM girlsStaff
WHERE (`blond` LIKE '%".$keyword."%') OR (`darkHair` LIKE '%".$keyword."%') OR (`busty` LIKE '%".$keyword."%') OR (`thin` LIKE '%".$keyword."%')" or die();


$query = mysqli_query($conx, $sql);


Now, apart from converting this code to PDO or prepared statement, there is another issue which I don't understand!


it doesn't matter how many chechboxes i select... it always returns the result for last checked/selected checkbox value from mysql database....


is there something that I am missing?


i also, did echo $keywords at the top of my page to see whats being sent to the page and I get the value of all the selected/checked boxes being sent correctly.. so I know the issue is not there.


any help or advice would be appreciated.





Aucun commentaire:

Enregistrer un commentaire