Jump to content

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


andrewc

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

Archived

This topic is now archived and is closed to further replies.

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