0

When I try to use the user_name from $_SESSION I get the error

'Undefined index: user_name'

CODE:

  $_SESSION['signed_in'] = true;

  while ($row = $query1->fetch(PDO::FETCH_ASSOC))
  {
    $_SESSION['user_id']    = $row['user_id'];
    $_SESSION['user_name']  = $row['user_first_name'];
  }

  echo 'Welcome, ' . $_SESSION['user_name'] . '. <a href="index.php">Proceed to the overview page to see your tasks</a>.';  
Rob Baillie
  • 3,436
  • 2
  • 20
  • 34
  • session_start() on the top of file, and isset($_SESSION) or var_dump do. – Smash Mar 30 '14 at 06:29
  • 1
    Have you added `session_start();` at the beginning ? –  Mar 30 '14 at 06:30
  • yeah I added session_start() and still not working – Oussamaa Mar 30 '14 at 06:35
  • Are you getting any data back from your query? Echo inside your while loop to see. – Rob Baillie Mar 30 '14 at 06:36
  • i am getting data and when i use var_dump on _$SESSION i get array (size=1) 'signed_in' => boolean true – Oussamaa Mar 30 '14 at 06:43
  • 1
    If you var_dump( $row ); inside the loop, what do you get? – Rob Baillie Mar 30 '14 at 07:11
  • And there's your problem. When you say nothing I presume you mean absolutely nothing. Meaning it never runs (var_dump never outputs absolutely nothing), because your query returns no data. Therefore you never actually set the values of user_id and user_name in $_SESSION. – Rob Baillie Mar 31 '14 at 06:11
  • I'd like to contest that this is a duplicate of the question referenced. The question referenced describes the generic issue of accessing a variable before it is set to a value and points towards declaring the variable. However, this is a LOGICAL error. There is an assumption that the `fetch` will return a row, when this is not always the case. I'd leave an answer to that effect with helpful code, but I can't as the answer has been marked as duplicate presumably by people who did not actually work out the problem and instead just saw "undefined index" and blindly marked it. – Rob Baillie Mar 31 '14 at 12:37

1 Answers1

1

Add session_start(); on top of your page to start the session.