0

I tried to execute this query -

$sql="INSERT INTO REGISTRATIONS VALUES ('$_SESSION['fname']', '$_SESSION['username']', '$_SESSION['height']', '$_SESSION['image']');";

And this one as well -

$sql="INSERT INTO REGISTRATIONS VALUES ($_SESSION['fname'], $_SESSION['username'], $_SESSION['height'], $_SESSION['image']);";

But both these returns an error. So i stored the session variables into normal php variables. and tried to execute the query -

$name = $_SESSION['fname'];
$username = $_SESSION['username'];
$height = $_SESSION['height'];
$image = $_SESSION['image'];

$sql="INSERT INTO REGISTRATIONS VALUES ('$name', '$username', '$height', '$image');";

And it worked.

But i want to know why the first two didn't work and why we have to save Session into another variable to get it to work?

Please help :)

1
  • would you mention the error? Commented Nov 22, 2013 at 10:27

3 Answers 3

3

Problem

You have have a syntax error in this code

$sql = "
    INSERT INTO REGISTRATIONS
    VALUES (
        '$_SESSION['fname']',
        '$_SESSION['username']',
        '$_SESSION['height']',
        '$_SESSION['image']'
    );
";

'$_SESSION['fname']' is causing an error here. You can't access array indexes inside a string without special syntax (see below).

Solution

Separate the string and variables with the concatenation operator, .

$sql = "
    INSERT INTO REGISTRATIONS
    VALUES ('"
    .$_SESSION['fname'].
    "', '"
    .$_SESSION['username'].
    "', '"
    .$_SESSION['height'].
    "','"
    .$_SESSION['image'].
    "');";

or use PHP's variable interpolation, see "Complex (curly) syntax". This is the prefered way.

$sql = "
    INSERT INTO REGISTRATIONS
    VALUES (
        '{$_SESSION['fname']}',
        '{$_SESSION['username']}',
        '{$_SESSION['height']}',
        '{$_SESSION['image']}'
    );
";

Please note, the this only works on double quoted strings.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Well explained. :)
2

Try these

$sql="INSERT INTO REGISTRATIONS VALUES ('".$_SESSION['fname']."', '".$_SESSION['username']."', '".$_SESSION['height']."', '".$_SESSION['image']."')";

Comments

0

you can use

echo "$_SESSION[name]";
echo "{$_SESSION['name']}";

see http://php.net/manual/en/language.types.string.php#language.types.string.parsing.simple

If you want to have a reason why this was done so, well, I can't help you there. But as always: PHP is old and has evolved a lot, thus introducing inconsistent syntax.

also check how to use associated array inside double quotes

http://php.net/manual/en/language.types.array.php#language.types.array.foo-bar

http://php.net/manual/en/language.types.string.php#language.types.string.parsing.complex

1 Comment

Thanks. Not exactly what i was looking for, but helped in one way. Also thanks for all the links.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.