Jump to content

dropdown (Multiple choices)


phpnewbie112

Recommended Posts

Hello I need pls ur advices and help on how can I do the following:

 

I have a mysql table with given values that I listed in a dropdown with multiple selection option (Values are Smoker, non-smoker, Tried it, etc...)

 

those values are also found in another table. what is the correct sql query syntax to use to query multiple selection for ex: if "Smoker" and "non-smoker" are selected both, I want to lookup the second table for BOTH values and display the results for smoker and non-smoker together.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/123667-dropdown-multiple-choices/
Share on other sites

Okay well your select name needs to have [] at the end, to make it an array.

 

<select name="selectBox[]">

 

Then you can create your query.

 

$selected = $_POST['selectBox'];
$where = "";

for($i = 0; $i < count($selected); $i++){
if($i < count($selected)-1){
	$where .= "columnName='".$selected[$i]."' AND ";
}else{
	$where .= "columnName='".$selected[$i]."'";
}
}

$query = "SELECT * FROM table WHERE {$where}";
echo $query;

Thank you

 

but it is returning the following

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'type='Smoker' AND type='non-smoker'' at line 1

 

the good news is that it is selecting both as shown in the syntax above

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.