Having and issue with passing a varible in a MySQL Q.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ATLChris
Forum Newbie
Posts: 15
Joined: Mon Feb 09, 2009 6:21 am
Location: Atlanta, GA

Having and issue with passing a varible in a MySQL Q.

Post by ATLChris »

Code: Select all

$location = $_POST['location'];
 
$query =  "UPDATE temp SET location='$location' WHERE sid='$session_id'";
$result =  mysql_query("$query") or die(mysql_error());
OK, this code has me stumped, if I echo location as is, it shows the contents from the form as expected, but when I process the form, the field updates in the SQL database, but with a blank instead of the varible. Now, if I change it to this:

Code: Select all

$location = $_POST['location'];
 
$query =  "UPDATE temp SET location='My Office' WHERE sid='$session_id'";
$result =  mysql_query("$query") or die(mysql_error());
It submits updates fine, so I know it is not the SQL Q, I have also run it through the database manually and it goes fine. It also works if I define location on this page:

Code: Select all

$location = 'My Office';
 
$query =  "UPDATE temp SET location='$location' WHERE sid='$session_id'";
$result =  mysql_query("$query") or die(mysql_error());
It has something todo with the $_POST in the MySQL statement. Please help everyone, this has me totally floored.
User avatar
andym01480
Forum Contributor
Posts: 390
Joined: Wed Apr 19, 2006 5:01 pm

Re: Having and issue with passing a varible in a MySQL Q.

Post by andym01480 »

Check your form spelling - to see the input field is the same!

also use mysql_real_escape_string() to make the query safer on both the $location and $session_id
User avatar
ATLChris
Forum Newbie
Posts: 15
Joined: Mon Feb 09, 2009 6:21 am
Location: Atlanta, GA

Re: Having and issue with passing a varible in a MySQL Q.

Post by ATLChris »

andym01480 wrote:Check your form spelling - to see the input field is the same!

also use mysql_real_escape_string() to make the query safer on both the $location and $session_id
I will try mysql_real_escape_string(). As for the form spelling, I can echo out the post data fine, so I know the form is submitting just fine.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Having and issue with passing a varible in a MySQL Q.

Post by VladSun »

Echo your query and see if it's what you expected it to be.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
ATLChris
Forum Newbie
Posts: 15
Joined: Mon Feb 09, 2009 6:21 am
Location: Atlanta, GA

Re: Having and issue with passing a varible in a MySQL Q.

Post by ATLChris »

The query echos fine. I can also take the echoed content and submit to the SQL manually through MySQL and it enters just fine. It seems like the issue with with the $_POST variable being passed through the mysql_query.
Last edited by ATLChris on Mon Feb 09, 2009 8:00 am, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Having and issue with passing a varible in a MySQL Q.

Post by VladSun »

ATLChris wrote:The query echos fine.
Can you post it?
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
ATLChris
Forum Newbie
Posts: 15
Joined: Mon Feb 09, 2009 6:21 am
Location: Atlanta, GA

Re: Having and issue with passing a varible in a MySQL Q.

Post by ATLChris »

andym01480 wrote:Check your form spelling - to see the input field is the same!

also use mysql_real_escape_string() to make the query safer on both the $location and $session_id

Code: Select all

$query = sprintf("UPDATE temp SET location='%s' WHERE sid='%s'",
            mysql_real_escape_string($location),
            mysql_real_escape_string($session_id));
Tried that and it didn't do anything.
UPDATE temp_capsule SET location='My Office' WHERE sid='123456'
This is what I get when I echo the query, all looks fine to me. If I enter 'My Office' in to the query fine, the table updates no problem, it is the variable causing the issue with the MySQL. Could I have something in the SQL messed up to where it wouldn't work?
User avatar
ATLChris
Forum Newbie
Posts: 15
Joined: Mon Feb 09, 2009 6:21 am
Location: Atlanta, GA

Re: Having and issue with passing a varible in a MySQL Q.

Post by ATLChris »

Just a FYI, I solved this issue. What it turned out to be was a Javascript loop that was causing my page to load 2 times. The first load it would update the database as it was suppose to, but the second time, it was clearing the variables and submitting the MySQL UPDATE again with no variables, so it was working fine, just Javascript was messing with it.
Post Reply