I want to be able to SELECT
only rows WHERE
all the submitted checkbox values
exist in keyWords
. My code SELECTS rows with any of the submitted checkbox values.
I've got a mySQL database PhotoArchive with a table photos
| id | title |filePath | keyWords |
| 1 | mypic1 |imgs/pic1.jpg | Mono,Portrait (etc) |
| 2 | mypic1 |imgs/pic2.jpg | Colour,Portrait (etc) |
| 3 | mypic2 |imgs/pic2.jpg | Mono,Landscape (etc) |
| 4 | mypic2 |imgs/pic2.jpg | Colour,Landscape (etc)|
I've got a html page with a checkbox form
<form action="selectFromDb.php" method="get">
<input type="checkbox" value="Mono" name="category[]" id="OptMono" />
<label for="OptMono"> Monochrome</label>
<input type="checkbox" value="Colour" name="category[]" id="OptColour" />
<label for="OptColour"> Colour</label>
<input type="checkbox" value="Portraits" name="category[]" id="OptPortraits" />
<label for="OptPortraits"> Portraits</label>
<input type="checkbox" value="Landscape" name="category[]" id="OptLandscape" />
<label for="OptLandscape"> Landscape</label>
<input type="submit" value="Submit" />
and selectFromDb.php file which uses this SELECT statement
<? foreach($_GET['category'] as $category){
$categories[] = $category;
}
$total_imgs_sql = "SELECT id, title, filePath FROM photos WHERE keyWords LIKE '%$category%'
?>
This returns the rows where any of the $category
values exist in keyWords
. I want to only return the rows WHERE
all the $category keyWords exist.
How can this be achieved?
(The full version has nearly 200 checkboxes and each row could have around 20 comma separated words in keyWords
)
Thanks