Search code examples
phpsqlsessionecho

Writing SQL query using data from $_SESSION


I am currently wanting to display the following on my page:
Hello, [First Name]!

As I want to have multiple users, I would need to use the session data, $_SESSION['UserID'], which I have set equal to the row $row['LoginID'], within my login process.

My database table looks like the following:
-------------- STAFF -------------
--- StaffID [PK] ---
--- First_Name ---
--- Last_Name ---

And this is where StaffID is a FK in the table Login:
------------ LOGIN ------------
--- LoginID [PK] ---
--- Username ---
--- Password ---
--- StaffID [FK] ---

What do I do in order to display the first name. I assume you will need a query, but I'm not sure how to specify the query using the login data from the session. Perhaps something along the lines like:

      $sesdata = $_SESSION['userID'];
  $name = "SELECT First_Name from Staff WHERE staff.StaffID = '.$sesdata.'";

  //$name = "SELECT First_Name from Staff WHERE StaffID = 2;";
  $query = mysqli_query($conn, $name);
    while($record = mysqli_fetch_assoc($query)){
      echo "<h1> Good Morning, ".$record['First_Name']."!</h1>";
    }

However, this does not echo anything. Any help is appreciated, thanks.


Solution

  • Thanks, user3783243, I only had to remove the '.' from .$sesdata for it to work.