iulo0566 Posted August 17, 2009 Share Posted August 17, 2009 Hello. I'm very new to php, and I'm running into the same problem over and over. I'm trying to get info from a database based on a variable (user defined). I've been testing this on Earthlink.net and also on inmotionhosting.com, but don't know what I'm missing. Any help would be greatly appreciated. Here is the search section of the code (not sure what other info you'll need to help me out): if ($searchtype || $searchterm) { $query = 'SELECT * FROM `rawmaterials` WHERE `$searchtype` LIKE `$searchterm` '; $result = mysql_query($query) or DIE("Could not Execute Query on table $usertable. "); echo '<table border="1">'; while ($row = mysql_fetch_array($result)) { echo '<tr><td colspan="2"><strong>Trade Name: </strong>'.'<br />'; echo $row['trade_name'].'<br />'; echo '<strong>Code #: </strong>'.$row['code_num'].'<br />'; echo '<strong>INCI Name: </strong>'.'<br />'; echo $row['INCI_name'].'<br />'; echo '<strong>Usage Levels: </strong>'.$row['usage_levels'].'<br />'; echo '<strong>Platform: </strong>'.$row['platform'].'<br />'; echo '</td>'; echo '<td colspan="4"><strong>Description: </strong>'.'<br />'; echo $row['description'].'<br />'; echo '</td>'; echo '</tr>'; } echo '</table>'; Link to comment https://forums.phpfreaks.com/topic/170702-beginner-return-query-with-user-input/ Share on other sites More sharing options...
rhodesa Posted August 17, 2009 Share Posted August 17, 2009 first thing i notice is this: $query = 'SELECT * FROM `rawmaterials` WHERE `$searchtype` LIKE `$searchterm` '; should be $query = "SELECT * FROM `rawmaterials` WHERE `$searchtype` LIKE '$searchterm'"; to make it syntactically correct...depending on what your logic is, it may need more changes also, when debugging, use mysql_error() to see what is wrong: $result = mysql_query($query) or DIE("Could not Execute Query on table $usertable: ".mysql_error()); Link to comment https://forums.phpfreaks.com/topic/170702-beginner-return-query-with-user-input/#findComment-900304 Share on other sites More sharing options...
ignace Posted August 17, 2009 Share Posted August 17, 2009 also, when debugging, use mysql_error() to see what is wrong: $result = mysql_query($query) or DIE("Could not Execute Query on table $usertable: ".mysql_error()); It is very important that you only use 'or die()' during debugging never ever use it in a production environment your visitors don't know what: "Could not Execute Query on table SomeTable: Query was empty" means. Always display a nicer message telling them what happened and what they can do next. Link to comment https://forums.phpfreaks.com/topic/170702-beginner-return-query-with-user-input/#findComment-900322 Share on other sites More sharing options...
iulo0566 Posted August 17, 2009 Author Share Posted August 17, 2009 thank you both. i changed the two lines of code you suggested, and the error i'm getting is " Unknown column 'aloe' in 'where clause" "aloe" was the $searchterm. not sure what to do, or why the error refers to "aloe" as a column Link to comment https://forums.phpfreaks.com/topic/170702-beginner-return-query-with-user-input/#findComment-900349 Share on other sites More sharing options...
rhodesa Posted August 17, 2009 Share Posted August 17, 2009 after $query = "SELECT * FROM `rawmaterials` WHERE `$searchtype` LIKE '$searchterm'"; if you do echo $query; what is the output? Link to comment https://forums.phpfreaks.com/topic/170702-beginner-return-query-with-user-input/#findComment-900352 Share on other sites More sharing options...
iulo0566 Posted August 17, 2009 Author Share Posted August 17, 2009 SELECT * FROM `rawmaterials` WHERE `platform` LIKE `extraction`Could not Execute Query on table rawmaterials: Unknown column 'extraction' in 'where clause' Link to comment https://forums.phpfreaks.com/topic/170702-beginner-return-query-with-user-input/#findComment-900363 Share on other sites More sharing options...
rhodesa Posted August 17, 2009 Share Posted August 17, 2009 you haven't updated this line: $query = "SELECT * FROM `rawmaterials` WHERE `$searchtype` LIKE '$searchterm'"; there needs to be single quotes around the $searchterm...not backticks Link to comment https://forums.phpfreaks.com/topic/170702-beginner-return-query-with-user-input/#findComment-900369 Share on other sites More sharing options...
iulo0566 Posted August 17, 2009 Author Share Posted August 17, 2009 o my goodness! it works!!!!!!!!!! i can't thank you enough! Link to comment https://forums.phpfreaks.com/topic/170702-beginner-return-query-with-user-input/#findComment-900385 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.