Reply
Passing a variable from a form to a MySql query
Old 06-14-2007, 06:47 AM Passing a variable from a form to a MySql query
Experienced Talker

Posts: 33
Trades: 0
I have a form set up with a dropdown list of values for a field "manufacturer" in my database

I can pass the variable to my php script. I know this because I use:

$manufacturer = $_POST['manufacturer'];

echo "<h1> Query result for manufacturer = ","$manufacturer", "</h1>";

And the page prints the appropriate value chosen from the form.
I'm trying to use the $manufacturer variable in a mysql query to select records which match.

My mysql query works when I use the following code:

$result = mysql_query("SELECT * FROM serialtest WHERE manufacturer = 'Yamaha'") or die(mysql_error());

but not when I add the $_POST[manufacturer] value in place of 'Yamaha' T
he error I get is: "Unknown column 'Yamaha' in 'where clause'"
Is it my syntax?


Many thanks for any help you can give.
Mark
monkey64 is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 06-14-2007, 08:21 AM Re: Passing a variable from a form to a MySql query
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 1,470
Name: Jeremy Miller
Location: Marianna, FL
Trades: 0
You probably wrote:

$result = mysql_query("SELECT * FROM serialtest WHERE manufacturer = ".$manufacturer) or die(mysql_error());


which removes the quotes around the entry, so try something like this:


$result = mysql_query("SELECT * FROM serialtest WHERE manufacturer = '".$manufacturer."'") or die(mysql_error());


Please note that you have not sanitized this user input against SQL injection in the code you've given us.
__________________
Jeremy Miller - TeraTask
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 06-17-2007, 08:07 AM Passing a variable from a form to a MySql query
Experienced Talker

Posts: 33
Trades: 0
This worked for me, thanks for the post.

$result = mysql_query("SELECT * FROM serialtest WHERE manufacturer = ('$_POST[serialnumber]')") or die(mysql_error());
monkey64 is offline
Reply With Quote
View Public Profile
 
Old 06-17-2007, 08:11 AM Re: Passing a variable from a form to a MySql query
JeremyMiller's Avatar
Full-Time TeraTasker

Posts: 1,470
Name: Jeremy Miller
Location: Marianna, FL
Trades: 0
That query is subject to SQL injection. You should look into protecting your data. I've written something up at www.programmerstalk.net/thread722.html .

I'm off on vacation starting tomorrow, so I won't be able to follow up for a week.
__________________
Jeremy Miller - TeraTask
Content Farmer - Automated Posting for Content & Blog Sites
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Reply     « Reply to Passing a variable from a form to a MySql query
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 



Page generated in 0.11774 seconds with 13 queries