...

View Full Version : How do I convert session variables to local variables?



Sherrie
01-08-2007, 11:41 AM
I'm sure this is a very simple solution. I have found a few threads with similar problems but I have tried the solutions (namely checking that the session_start() was on every page and putting the { in front of the session variable, but both don't seem to be working for me.

As such, how do I convert a session variable to a local variable?

I've currently got this code:


$los=$_SESSION['answer'][los];

$los remains empty though.

When I tried the following, I got a parse error saying that there was an unexpected '{' on the line where i tried to convert it.


$age={$_SESSION['answer'][age]};

Can someone please tell me how to do this?

I've stored my session variables and have checked, and they work perfectly.

The problem I'm having is in referring to my session variables in my SQL statement


$query="INSERT INTO HGsurvey
VALUES ('$_SESSION['answer'][age]','$_SESSION['answer'][los]','$_SESSION['answer'][eecat]')";
mysql_query($query)
or DIE ("Couldn't insert questionnaire");

I kept getting a parse error that it was expecting a T_Variable and something about whitespace.

As such, I'm trying to assign the session variable to a local variable so that I could do this instead:


$query="INSERT INTO HGsurvey
VALUES ('$age',$los','eecat')";
mysql_query($query)
or DIE ("Couldn't insert questionnaire");

I tested it using echo, but while the session variable returned the value submitted in the form, the local variable returned an empty value.

As a note, I have ensured that every page has session_start(); at the top.

Thanks again!

koyama
01-08-2007, 01:15 PM
If $los is empty after this statement (syntax is ok)


$los=$_SESSION['answer'][los];

then the query part will not work in any case. So you should resolve this first. Try to do this


var_dump($_SESSION);

at the beginning of your script to get the full view of your session variables. I recommend that you for now write like this:


$los = $_SESSION['answer']['los']; // instead of $_SESSION['answer'][los]

or when used in expressions with double quotes use braces like this:


$los = "{$_SESSION['answer']['los']}";

Both should work.

Sherrie
01-08-2007, 01:40 PM
koyoma,

Thank you for your reply!

Will try that out and let you know how it goes :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum