I'm trying to make users be automatically logged in when they register, so they don't have to log in after they register, which is happening with my code right now.

Here is my code, starting from right after I check if one of the fields are blank.

else if (query("INSERT INTO users (username, hash, cash) VALUES(?, ?, 10000.00)", $_POST["username"], crypt($_POST["password"])) !== false)
            $rows = query("SELECT LAST_INSERT_ID() AS id");
            $id = $rows[0]["id"];
            // query database for user
            $rows = query("SELECT * FROM users WHERE username = ?", $_POST["username"]);
            // remember that user's now logged in by storing user's ID in session
            $_SESSION["id"] = $row["id"];
            // redirect to portfolio
  • 1
    seems to me your problem is in $_SESSION["id"] = $row["id"]; what is $row[id]? i think should be $_SESSION["id"] = $id;
    – wallek876
    Jul 20, 2015 at 19:44
  • @wallek876 Please write that as an answer, because it works and so I can up vote it.
    – i_am_david
    Jul 20, 2015 at 20:05

1 Answer 1


The problem is in:

$_SESSION["id"] = $row["id"];

it should be:

$_SESSION["id"] = $id;

You must log in to answer this question.

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