andrewc Posted September 4, 2007 Share Posted September 4, 2007 Hi, Apologies in advance for the basic level of this question. I'm no programmer and am trying to sort out a problem for a friend. I have made a website for a local college with a MySQL database of available courses. I want users to be able to search for courses by region. So far, I have created a drop-down box of options in a html form - and a handle_form page to handle the form. My problem is that I can't link the variable created from the form with the MySQL query. The relevant php code from the handle_form page is as follows: $var = ($_POST['region']); //retrieve the variable using superglobals require_once ('mysql_connect.php'); // Connect to the db. // Make the query. $query = 'SELECT * FROM Test WHERE Region =\'west\''; $rt = @mysql_query ($query); // Run the query. while($nt=mysql_fetch_array($rt)){ echo "<b>Branch: $nt[branch]</b>"; } I want to replace the \'west\' condition with $var so users can see the branches in the selected region - but I don't know how! Not sure if it is simple sytnax I require or to rewrite the query. Have just spent two days trying syntax variations and trawling internet for example scripts but no luck. Any help, tips or advice would be gratefully received. Quote Link to comment https://forums.phpfreaks.com/topic/67895-solved-newbie-question-assigning-form-variables-in-mysql-query/ Share on other sites More sharing options...
Ninjakreborn Posted September 4, 2007 Share Posted September 4, 2007 require_once ('mysql_connect.php'); // Connect to the db. $var = mysql_real_escape_string($_POST['region']); //retrieve the variable using superglobals // Make the query. $query = "SELECT * FROM Test WHERE Region ='$var'"; $rt = mysql_query($query); // Run the query. while($row = mysql_fetch_array($rt)) { echo "Branch: $row[branch]"; } Quote Link to comment https://forums.phpfreaks.com/topic/67895-solved-newbie-question-assigning-form-variables-in-mysql-query/#findComment-341262 Share on other sites More sharing options...
andrewc Posted September 4, 2007 Author Share Posted September 4, 2007 Thanks a million! The adult learners of the county of Essex, UK will be eternally in your debt... Quote Link to comment https://forums.phpfreaks.com/topic/67895-solved-newbie-question-assigning-form-variables-in-mysql-query/#findComment-341271 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.