-2

Single quotes inside single quotes in php, I don't know how to get the $_SESSION variable inside a string. Having issues with the formatting of this line.

I have been looking at multiple answers similar, but got none of them working:

How do I use single quotes inside single quotes?

syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE) in Insert statement

$sql = "INSERT INTO pushtable (uidUsers, pushAntall, pushDato, pushKlokkeslett) VALUES ('$_SESSION['uid']', '$pushAntall', '$pushDato', '$pushKlokkeslett')";

ERROR

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING)

Also tried:

$sql = "INSERT INTO `pushtable` (`uidUsers`, `pushAntall`, `pushDato`, `pushKlokkeslett`) VALUES ('{$_SESSION['uid']}', '$pushAntall', '$pushDato', '$pushKlokkeslett');";

ERROR

Notice: Undefined variable: _SESSION

How can i solve this?

-SOLVED-

$sql = "INSERT INTO pushtable (uidUsers, pushAntall, pushDato, pushKlokkeslett) VALUES ('"·$_SESSION['uid'] ."', '$pushAntall', '$pushDato', '$pushKlokkeslett')";

Asphaug
  • 21
  • 1
  • 7
  • Well, you can always assign it to a variable first – Carl Binalla Oct 10 '19 at 09:23
  • @CarlBinalla I tried doing this: ```$username = $_SESSION['uid'];```. Did not work.. – Asphaug Oct 10 '19 at 09:25
  • have you start the session? – KUMAR Oct 10 '19 at 09:26
  • so insert `$username` instead of `$_SESSION['uid']` – KUMAR Oct 10 '19 at 09:27
  • @Asphaug Elaborate **Did not work**. Did you received the same error? Did you actually replaced it with `$username`? – Carl Binalla Oct 10 '19 at 09:28
  • @KUMAR. Yup, i can do ```echo $_SESSION['uid'];``` and get the uid echoed into the html website. But the problem is the formatting f the line becouse of all the singel quotes. – Asphaug Oct 10 '19 at 09:28
  • One more thing ,I really suggest using a prepared statement – Carl Binalla Oct 10 '19 at 09:28
  • @CarlBinalla ```$username = $_SESSION['uid'];``` with the new line: ```$sql = "INSERT INTO `pushtable` (`uidUsers`, `pushAntall`, `pushDato`, `pushKlokkeslett`) VALUES ('$username', '$pushAntall', '$pushDato', '$pushKlokkeslett');";``` still throw error number 2. – Asphaug Oct 10 '19 at 09:28
  • @CarlBinalla, I dont really care about prepared statements since its just local testing, and this is not going online. – Asphaug Oct 10 '19 at 09:30
  • Look at my earlier comment. Posted it with accident and edited it now. @CarlBinalla – Asphaug Oct 10 '19 at 09:34
  • Well, did you try concatenating it instead? – Carl Binalla Oct 10 '19 at 09:36
  • Suddenly got it working. $sql = "INSERT INTO pushtable (uidUsers, pushAntall, pushDato, pushKlokkeslett) VALUES ('"·$_SESSION['uid'] ."', '$pushAntall', '$pushDato', '$pushKlokkeslett')"; did the trick. Thanks guys! – Asphaug Oct 10 '19 at 09:46

1 Answers1

-1

An elegant solution is to use sprinf sprintf

$sql = sprintf("INSERT INTO pushtable (uidUsers, pushAntall, pushDato, 
pushKlokkeslett) VALUES ('%s', '%s', '%s', '%s')",
$_SESSION['uid'],
$pushAntall,
$pushAntall,
$pushKlokkeslett
);

Or fix your code:

$sql = "INSERT INTO pushtable (uidUsers, pushAntall, pushDato, pushKlokkeslett) VALUES ('". $_SESSION['uid'] ."', '$pushAntall', '$pushDato', '$pushKlokkeslett')";

Also check string operators

TheNick
  • 69
  • 1
  • 5
  • Went for the "fix your code" solution, still throws: **ERROR** >Parse error: syntax error, unexpected '·' (T_STRING) in when i do ```$sql = "INSERT INTO pushtable (uidUsers, pushAntall, pushDato, pushKlokkeslett) VALUES ('"·$_SESSION['uid'] ."', '$pushAntall', '$pushDato', '$pushKlokkeslett')";``` – Asphaug Oct 10 '19 at 09:36
  • And the sprinf still throws "Notice: Undefined variable: _SESSION". I can still echo the uid. – Asphaug Oct 10 '19 at 09:38
  • @Asphaug You are not using a `.` here -> `"·$` – Carl Binalla Oct 10 '19 at 09:39
  • @CarlBinalla Sorry. Replaced the wierd dot with a normal dot. ```$sql = "INSERT INTO pushtable (uidUsers, pushAntall, pushDato, pushKlokkeslett) VALUES ('".$_SESSION['uid'] ."', '$pushAntall', '$pushDato', '$pushKlokkeslett')";``` Still throws "Notice: Undefined variable: _SESSION in" – Asphaug Oct 10 '19 at 09:43
  • Suddenly got it working. ```$sql = "INSERT INTO pushtable (uidUsers, pushAntall, pushDato, pushKlokkeslett) VALUES ('"·$_SESSION['uid'] ."', '$pushAntall', '$pushDato', '$pushKlokkeslett')";``` did the trick. Thanks guys! – Asphaug Oct 10 '19 at 09:46
  • I editted the answer to replace · with . ! – TheNick Oct 10 '19 at 10:05