nanjero05 nanjero05 - 6 months ago 28
SQL Question

PHP/SQL - unexpected T_ENCAPSED_AND_WHITESPACE error

this is the code that is getting the error:

session_start();
$user = $_SESSION["username"];

$sql = "SELECT cost FROM rooms
WHERE rid = $rNumber";
$results = mysqli_query($conn, $sql)
or die ('Problem with query' . mysqli_error($conn));

$row = mysqli_fetch_array($results);
$sql2 = "INSERT INTO bookings (rid, username, checkin, checkout, cost)
VALUES ($rNumber, $user, $cInDate, $cOutDate, $row['price'])"; //this line is where the error occurs
$results2 = mysqli_query($conn, $sql2)
or die ('Problem with query' . mysqli_error($conn));


I seem to be getting this error :

unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING


I have tried checking other posts about the same error and I gathered that i have to use the (.) to concatenate something but im not sure what. I have echoed the variables just before the sql and they are present.

Answer

Please change that line like this.

$sql2 = "INSERT INTO bookings (rid, username, checkin, checkout, cost)
VALUES ('$rNumber', '$user', '$cInDate', '$cOutDate', '{$row['price']}')";

I also solved MySql syntax error from your query :P