0

Hi, I am working on a refined search feature and I am having a little problem with the following code. I'm trying to get the "genre" values from the form. Then use them in a query to refine the results from the database. The problem is as follows.

I need "$str" to pass the values from the form like this:
AND genre IN ('other','metal','pop')';

But, with multiple selected boxes "$str" passes them like this:
AND genre IN ('other,metal,pop')';


Any help would be great!

SIDE NOTE:If one checkbox is selected, it works fine.

<?
	if(isset($_REQUEST['submit']))
	{		
		$str = ' 1=1 ';		
		
		if( count($_POST['genre']) > 0 )
		{		
			$genre =  implode(',',$_POST['genre']); 	
			$str.= ' AND genre IN ('.$genre.')';

                        echo $sql = "SELECT * FROM uploads WHERE ".$str;
		}	
}
?>
<form name="form" id="form" method="post" action="">
genre:
<input type="checkbox" checked="checked" name="genreAny[]" value="Any">Any
<input type="checkbox" name="genre[]" value="other">other
<input type="checkbox" name="genre[]" value="metal">metal
<input type="checkbox" name="genre[]" value="pop">pop
<br>
<input name="submit" value="Search" type="submit">
</form>
3
Contributors
3
Replies
6
Views
6 Years
Discussion Span
Last Post by dietdew12z
0

I haven't tried it(I will, though) but on line 8 I wonder if you did this:

$genre =  implode("','",$_POST['genre']);

I'll go try that and see if I gave you crappy advice. :)

David

0

I haven't tried it(I will, though) but on line 8 I wonder if you did this:

$genre =  implode("','",$_POST['genre']);

I'll go try that and see if I gave you crappy advice. :)

David

Hehe not that should be

$genre =  implode(",",$_POST['genre']);

But I don't know where is the problem actually :/

0

:) I was close.

Here is what I had to do:

$genre =  implode("','",$_POST['genre']); 	
			$str.= " AND genre IN ('".$genre."')";

And it output:
SELECT * FROM uploads WHERE 1=1 AND genre IN ('other','metal')

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.