Jump to content

[SOLVED] Newbie question - assigning form variables in MySQL query


Recommended Posts

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.

 

 

   
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]";     
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.