Why does the database not return data about the user after registering?
<p>I recently learned how to make login systems with PHP and MYSQL. I decided to make one for my site. The user signs up with email, password, first name, and last name. I wanted to make a greeting say "My greetings, " and then their name. I could save the names in a session, but I am wondering why my method doesn't completely work. It works every time EXCEPT right after registration. This is the complete signup code:</p>

<pre><code>$error = "";

include("loginsession.php");


    if(array_key_exists("id", $_SESSION)){

        header("Location: index.php");

    }

if(array_key_exists("submit", $_POST)){

    include("connection.php");//database connection



    $email = $_POST['email'];

    $password = $_POST['password'];

    $firstName = $_POST['firstname'];

    $lastName = $_POST['lastname'];

    $query = "SELECT * FROM users WHERE email='".mysqli_real_escape_string($link, $email)."'";

    $result = mysqli_query($link, $query);

    if(mysqli_num_rows($result) &gt; 0){

        $error =  "&lt;div class='alert alert-danger' role='alert'&gt;&lt;strong&gt;Error:&lt;br&gt;&lt;br&gt;&lt;/strong&gt;This email already exists.&lt;hr&gt;&lt;/div&gt;";

    }else{

        $query = "INSERT INTO users(firstname, lastname, email, password) VALUES('".mysqli_real_escape_string($link, $firstName)."', '".mysqli_real_escape_string($link, $lastName)."', '".mysqli_real_escape_string($link, $email)."', '".mysqli_real_escape_string($link, $password)."')";

        mysqli_query($link, $query);

        $query = "UPDATE users SET password = '".md5(md5(mysqli_insert_id($link)).$password)."' WHERE id=".mysqli_insert_id($link)."";

        mysqli_query($link, $query);


    $_SESSION['id'] = mysqli_insert_id($link);//where the issue originates
    if(isset($_POST['check']) &amp;&amp; $_POST['check']=='true'){

        setcookie("id", mysqli_insert_id($link), time() + 60*60*24*365);

    }
    header("Location: index.php");

    }        

}
</code></pre>

<p>On the index page, this is what I do:</p>

<pre><code>    session_start();
    if(array_key_exists('id', $_COOKIE)){

        $_SESSION['id'] = $_COOKIE['id'];   

    }


    $greeting = "";

        if(array_key_exists('id', $_SESSION)){

            include("connection.php");//just the login to the database

            $query = "SELECT * FROM users WHERE id = ".$_SESSION['id'];

            $results = mysqli_query($link, $query);

            $row = mysqli_fetch_array($results);

        $greeting = "My greetings, ".$row['firstname']." ".$row['lastname'];//add name to greeting variable

        }
</code></pre>

<p>Later in the code:</p>

<pre><code>&lt;p id="greeting"&gt;
    &lt;?php echo $greeting; ?&gt;
&lt;/p&gt;
</code></pre>

<p>The right name shows up after logging in, but never after signing up, even if I reload the page. What shows up instead of "My greetings, firstname lastname" is "My greetings, ". It is like the database found no name for that particular id. </p>

<p>How can I have the correct result right after registering. If you need any other code from the signup or login process, please tell me and I will post it. If anyone wants to try this for themselves, my website is <a href="http://msolonko.net" rel="nofollow">here</a>.</p>