86Stang Posted January 19, 2008 Share Posted January 19, 2008 Hi All, I've been working on a search page even though I was getting some GREAT advice from some of you, I've decided to start over. The code was a complete mess and I could never really get anywhere with it on my own so .... Anyway, I've rebuilt it and it seems to be working fine but it won't recognize the options selected from dropdown menus. I get a "Unknown column in where clause" error. I'm populating the dropdowns from arrays. The search code: $qry = "SELECT * FROM table WHERE description LIKE '%$keywords%'"; if ($_POST['location'] != '') { $qry .= " AND location = " . $location; } $qry .= " ORDER BY ad_date DESC"; $result = mysql_query($qry) or die(mysql_error()); The dropdown: <select name="location"> <? echo "<option value=\"\">Choose A Location</option>\n"; for ($i=0;$i<count($locations);$i++) { if ($locations[$i] == $location) { echo "<option value=\"$locations[$i]\" selected>$locations[$i]</option>\n"; } else { echo "<option value=\"$locations[$i]\">$locations[$i]</option>\n"; } } ?> </select> And the locations array: $locations = array('Boston','New London','New York'); And I have a "location" field in the database. Any idea why this is happening? Quote Link to comment https://forums.phpfreaks.com/topic/86814-solved-unknown-column-in-where-clause/ Share on other sites More sharing options...
toplay Posted January 19, 2008 Share Posted January 19, 2008 I get a "Unknown column in where clause" error. I'm populating the dropdowns from arrays. That error means that you names your column wrong or have defined the column in your SQL query. For instance the table created contains a column called "desc" but in your query you called it "description". Quote Link to comment https://forums.phpfreaks.com/topic/86814-solved-unknown-column-in-where-clause/#findComment-443697 Share on other sites More sharing options...
86Stang Posted January 19, 2008 Author Share Posted January 19, 2008 Hmmm. Well, the exact message I'm getting is Unknown column 'Boston' in 'where clause' Replace 'Boston' with whichever location I select from the dropdown. The database has a column titled 'location' with records within named Boston, the dropdown is named 'location' and the resulting query looks like: SELECT * FROM table WHERE description LIKE '%any%' AND location = Boston ORDER BY ad_date DESC I'm stumped... Quote Link to comment https://forums.phpfreaks.com/topic/86814-solved-unknown-column-in-where-clause/#findComment-443723 Share on other sites More sharing options...
PFMaBiSmAd Posted January 19, 2008 Share Posted January 19, 2008 String values need single-quotes around them so that mysql knows they are strings instead of column names. Quote Link to comment https://forums.phpfreaks.com/topic/86814-solved-unknown-column-in-where-clause/#findComment-443732 Share on other sites More sharing options...
toplay Posted January 19, 2008 Share Posted January 19, 2008 Use quotes around string values: SELECT * FROM table WHERE description LIKE '%any%' AND location = 'Boston' ORDER BY ad_date DESC Quote Link to comment https://forums.phpfreaks.com/topic/86814-solved-unknown-column-in-where-clause/#findComment-443733 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.