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>

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

Member Avatar for TechySafi

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 :/

:) 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')

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.