Page 1 of 1
Having and issue with passing a varible in a MySQL Q.
Posted: Mon Feb 09, 2009 6:26 am
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.
Re: Having and issue with passing a varible in a MySQL Q.
Posted: Mon Feb 09, 2009 6:55 am
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
Re: Having and issue with passing a varible in a MySQL Q.
Posted: Mon Feb 09, 2009 6:58 am
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.
Re: Having and issue with passing a varible in a MySQL Q.
Posted: Mon Feb 09, 2009 7:00 am
by VladSun
Echo your query and see if it's what you expected it to be.
Re: Having and issue with passing a varible in a MySQL Q.
Posted: Mon Feb 09, 2009 7:54 am
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.
Re: Having and issue with passing a varible in a MySQL Q.
Posted: Mon Feb 09, 2009 7:57 am
by VladSun
ATLChris wrote:The query echos fine.
Can you post it?
Re: Having and issue with passing a varible in a MySQL Q.
Posted: Mon Feb 09, 2009 8:07 am
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?
Re: Having and issue with passing a varible in a MySQL Q.
Posted: Thu Feb 12, 2009 3:12 pm
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.