-1

I'm trying to select a row using a session variable which is already defined, but when I try it comes up with the error:

Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting '-' or identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in viewmybookings.php on line 9

I have tried defining the session variable as a PHP variable and using the PHP variable however it still doesn't work

<?php
//first connect to the the database via your connection insert file
  include'db.php';
    $sql = "SELECT lesson.LessonName,lesson.LessonType,bookingtable.LessonDate,bookingtable.LessonStartTime,bookingtable.Duration,bookingtable.Statues FROM lesson
JOIN bookingtable
ON bookingtable.LessonID=lesson.Id
JOIN users
ON users.UserID = bookingtable.UserID
WHERE users.UserFirstName = <?php $_SESSION['UserID']?>";
//line 9 is the last line
?>
Masoud Rahimi
  • 5,785
  • 15
  • 39
  • 67

2 Answers2

-1

You dont need the opening PHP tag, since you are already in a PHP statement. All you need to do, is to append it to your query.

<?php
//first connect to the the database via your connection insert file
include 'db.php';
$sql = "SELECT lesson.LessonName,lesson.LessonType,bookingtable.LessonDate,bookingtable.LessonStartTime,bookingtable.Duration,bookingtable.Statues FROM lesson
JOIN bookingtable
ON bookingtable.LessonID=lesson.Id
JOIN users
ON users.UserID = bookingtable.UserID
WHERE users.UserFirstName = " . $_SESSION['UserID'];
//line 9 is the last line
?>

This query has an opening to SQL injection (its very unlikely someone guesses your session id, but possible), which you could avoid using prepared statements.

Manuel Mannhardt
  • 2,191
  • 1
  • 17
  • 23
  • I have tried that however i got this error instead Bad Query: SELECT lesson.LessonName,lesson.LessonType,bookingtable.LessonDate,bookingtable.LessonStartTime,bookingtable.Duration,bookingtable.Statues FROM lesson JOIN bookingtable ON bookingtable.LessonID=lesson.Id JOIN users ON users.UserID = bookingtable.UserID WHERE users.UserFirstName = – Abdullah Almazmome May 09 '19 at 09:03
-1

You can simply use any variable in the query like this:

$userId = $_SESSION['UserID'];
 $sql = "SELECT lesson.LessonName,lesson.LessonType,bookingtable.LessonDate,bookingtable.LessonStartTime,bookingtable.Duration,bookingtable.Statues FROM lesson
JOIN bookingtable
ON bookingtable.LessonID=lesson.Id
JOIN users
ON users.UserID = bookingtable.UserID
WHERE users.UserFirstName = '$userId'";

Please be aware of SQL injection.

Aaron
  • 1,600
  • 1
  • 8
  • 14