phpnewbie112 Posted September 10, 2008 Share Posted September 10, 2008 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 More sharing options...
phpnewbie112 Posted September 11, 2008 Author Share Posted September 11, 2008 any advices? Link to comment https://forums.phpfreaks.com/topic/123667-dropdown-multiple-choices/#findComment-638905 Share on other sites More sharing options...
JasonLewis Posted September 11, 2008 Share Posted September 11, 2008 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; Link to comment https://forums.phpfreaks.com/topic/123667-dropdown-multiple-choices/#findComment-638916 Share on other sites More sharing options...
phpnewbie112 Posted September 11, 2008 Author Share Posted September 11, 2008 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 Link to comment https://forums.phpfreaks.com/topic/123667-dropdown-multiple-choices/#findComment-638923 Share on other sites More sharing options...
phpnewbie112 Posted September 11, 2008 Author Share Posted September 11, 2008 I added ` ` $where .= "`columnName`='".$selected[$i]."' AND "; and replaced AND with OR it gave the result. do you think it's a good idea? Link to comment https://forums.phpfreaks.com/topic/123667-dropdown-multiple-choices/#findComment-638926 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.