0

I am getting aerror while concatenating query for SQL in PHP

$query = "INSERT INTO bookings (date, start, userId)
VALUES ('$booking_date','$booking_time','$_SESSION['userIdSession']');";

The single quotes '' in $_SESSION['userIdSession'] are causing this problem.

What is the right way to concatenate this string without getting error?

(I am beginner in PHP, do not down vote me)

2 Answers2

0

This is most likely what you are looking for:

$query = "INSERT INTO bookings (date, start, userId)
VALUES ('" . $booking_date . "','" . $booking_time . "','" . $_SESSION['userIdSession'] . "');";

However always make sure you are protecting against SQL Injections, else you might end up with serious problems if someone manage to mess with your database through unprotected user input.

  • What's wrong with curly braces? – Marcel Korpel Dec 22 '13 at 20:30
  • First of all SO removed them automaticly, besides it is better without and then have that formating as I just posted. The bad thing when using them is to make sure the server and mysql is supporting and allowing the use of them. – Søren Kjeldsen Dec 22 '13 at 20:33
-1

When using array variables in double-quoted strings, you must surround them with curly brackets:

$query = "INSERT INTO bookings (date, start, userId)
    VALUES ('$booking_date','$booking_time','{$_SESSION['userIdSession']}');";

Otherwise PHP will not correctly recognize the array access in the string.

Additionally, you must prevent SQL Injection as stated in the comments above.

BluePsyduck
  • 1,111
  • 9
  • 9