Larry101 Posted April 20, 2011 Share Posted April 20, 2011 Hi all... once again I am trying to re-educate my self into PHP after a long gap. I do not have a problem as such just a question... here is part of my script that doesnot work; <? $sql= "INSERT INTO member ( username, email ) VALUES ( \"$_SESSION['nm_username']\", \"$_SESSION['nm_email']\" )"; ?> The above errors because there are single quotation marks in the session variables. When I remove them the script works and the values of the variables seem to be correct! My question is - do I NEED the quotation marks in the variable and if so how should I write the query? Regards Link to comment https://forums.phpfreaks.com/topic/234256-quotation-marks-on-session-variables/ Share on other sites More sharing options...
Muddy_Funster Posted April 20, 2011 Share Posted April 20, 2011 Yes, you need the quotes around the SESSION[] elements. You should also only use single quotes around sql strings. your query should look like: $sql= "INSERT INTO member ( username, email ) VALUES ( '".$_SESSION['nm_username']."', '".$_SESSION['nm_email']."' )"; using . to concatenate the string values with the variable contents. Link to comment https://forums.phpfreaks.com/topic/234256-quotation-marks-on-session-variables/#findComment-1203992 Share on other sites More sharing options...
Larry101 Posted April 20, 2011 Author Share Posted April 20, 2011 Yes, you need the quotes around the SESSION[] elements. You should also only use single quotes around sql strings. your query should look like: $sql= "INSERT INTO member ( username, email ) VALUES ( '".$_SESSION['nm_username']."', '".$_SESSION['nm_email']."' )"; using . to concatenate the string values with the variable contents. Thank you very much Muddy_Funster. I didn't feel comfortable leaving off the quotations. Link to comment https://forums.phpfreaks.com/topic/234256-quotation-marks-on-session-variables/#findComment-1204000 Share on other sites More sharing options...
Muddy_Funster Posted April 20, 2011 Share Posted April 20, 2011 no worries Link to comment https://forums.phpfreaks.com/topic/234256-quotation-marks-on-session-variables/#findComment-1204005 Share on other sites More sharing options...
KevinM1 Posted April 20, 2011 Share Posted April 20, 2011 For interpolating array values, you can also use curly braces around the array value while within in the string. Like so: echo "Look, I'm an unbroken string! Here's my data: {$_SESSION['someData']}"; Link to comment https://forums.phpfreaks.com/topic/234256-quotation-marks-on-session-variables/#findComment-1204012 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.