0

I am having some issues getting redirected to index.php after adding the user to the database. Everything seems to be running just fine but I am not being redirected. Do I have to implement more parts of the PSET before it will work or do I have a mistake?

Code link: https://gist.github.com/anonymous/2f26599663af7ccd0d28bfb84f5263a8

1 Answer 1

1

I'd add a debug line like this:

dump($_SESSION);

on line 40.

You might be surprised to find that you are not storing the user's ID in the $_SESSION. Why not?

Because even though SQL statements are not case-sensitive (and are traditionally entered in ALL CAPS), PHP variables most certainly are.

Therefore, when you ask for the database to return this:

    $rows = CS50::query("SELECT LAST_INSERT_ID() AS ID");

... and then check your result using this array index:

    $_SESSION["id"] = $rows[0]["id"];

... you won't find the expected value at $rows[0]["id"].

The fix is either to ask the database to return this:

    $rows = CS50::query("SELECT LAST_INSERT_ID() AS id");

... or to tell PHP that the array index is this:

    $_SESSION["id"] = $rows[0]["ID"];

But don't change both of them, or you'll have the same problem!

EDIT: I'm not sure if that is the reason why your redirect is not working. Let me know if it helps!

1
  • 1
    thanks for the help. That made pretty good sense, I glossed over it because of the SQL and did not know how to output to debug...
    – Zach B
    Sep 7, 2016 at 3:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .