Re: [PHP] Having Trouble With Session Variable in Query Statement

2009-02-23 Thread Micah Gersten
Paul M Foster wrote:
> On Mon, Feb 23, 2009 at 12:34:58PM -0800, revDAVE wrote:
>
>   
>> Hi folks,
>>
>> I'm trying to make an update query with a session variable...
>>
>> It creates this error:
>>
>> Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting
>> T_STRING or T_VARIABLE or T_NUM_STRING in ...
>>
>> Q: the session var shows ok on the page :
>> ID 
>> - so how do I fix the error?
>>
>> ==
>>
>> > if(!session_id()) session_start();
>> $amt = 18;
>> $id1 = 8;
>> $_SESSION['thisid']=8;
>> $id2 = $_SESSION['thisid'] ;
>>
>>
>>   //these 3 work
>> $updateSQL ="UPDATE `mytable` SET thetotal=$amt WHERE id=8";
>> $updateSQL ="UPDATE `mytable` SET thetotal=$amt WHERE id=$id1";
>> $updateSQL ="UPDATE `mytable` SET thetotal=$amt WHERE id=$id2"; // uses
>> $_SESSION['thisid']
>>
>> //but this does not..
>> $updateSQL ="UPDATE `mytable` SET thetotal=$amt WHERE
>> id=$_SESSION['thisid']";
>> 
>
> Don't single quote values inside array brackets when the whole
> expression is in double quotes. You've got:
>
> "... $_SESSION['thisid']";
>
> Do this instead:
>
> "... $_SESSION[thisid]";
>
> Paul
>   

Better off doing this so you don't get into the habit of not using
quotes around array params:

"... {$_SESSION['thisid']}";

See this:
http://us2.php.net/manual/en/language.types.string.php#language.types.string.parsing

Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com





Re: [PHP] Having Trouble With Session Variable in Query Statement

2009-02-23 Thread revDAVE
On 2/23/2009 12:44 PM, "Paul M Foster"  wrote:

> Don't single quote values inside array brackets when the whole
> expression is in double quotes. You've got:
> 
> "... $_SESSION['thisid']";
> 
> Do this instead:
> 
> "... $_SESSION[thisid]";


AHA - thanks much - that worked!



--
Thanks - RevDave
Cool @ hosting4days . com
[db-lists 09]




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Having Trouble With Session Variable in Query Statement

2009-02-23 Thread Philip Graham


On February 23, 2009 15:34:58 revDAVE wrote:
> Hi folks,
>
> //but this does not..
> $updateSQL ="UPDATE `mytable` SET thetotal=$amt WHERE
> id=$_SESSION['thisid']";
>
> Q: How can I fix it?

try :
 $updateSQL ="UPDATE `mytable` SET thetotal=$amt WHERE
  id={$_SESSION['thisid']}";

or even better:
 $updateSQL ="UPDATE `mytable` SET thetotal=".mysql_real_escape_string($amt).
  " WHERE id=".mysql_real_escape_string($_SESSION['thisid']);

substituting mysql_real_escape_string with the escape function for your 
database.
-- 
Philip Graham
Lightbox Technologies
Suite 312 240 Catherine St.
Ottawa, ON, K2P 2G8
613-686-1661 ext. 102

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Having Trouble With Session Variable in Query Statement

2009-02-23 Thread Paul M Foster
On Mon, Feb 23, 2009 at 12:34:58PM -0800, revDAVE wrote:

> Hi folks,
> 
> I'm trying to make an update query with a session variable...
> 
> It creates this error:
> 
> Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting
> T_STRING or T_VARIABLE or T_NUM_STRING in ...
> 
> Q: the session var shows ok on the page :
> ID 
> - so how do I fix the error?
> 
> ==
> 
>  if(!session_id()) session_start();
> $amt = 18;
> $id1 = 8;
> $_SESSION['thisid']=8;
> $id2 = $_SESSION['thisid'] ;
> 
> 
>   //these 3 work
> $updateSQL ="UPDATE `mytable` SET thetotal=$amt WHERE id=8";
> $updateSQL ="UPDATE `mytable` SET thetotal=$amt WHERE id=$id1";
> $updateSQL ="UPDATE `mytable` SET thetotal=$amt WHERE id=$id2"; // uses
> $_SESSION['thisid']
> 
> //but this does not..
> $updateSQL ="UPDATE `mytable` SET thetotal=$amt WHERE
> id=$_SESSION['thisid']";

Don't single quote values inside array brackets when the whole
expression is in double quotes. You've got:

"... $_SESSION['thisid']";

Do this instead:

"... $_SESSION[thisid]";

Paul
-- 
Paul M. Foster

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php