Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Pieter Bracke
Pieter Bracke
4,078 Points

How to refresh session variables without loggin out and in

Today I was working on an update script so my users could update their own data, This is finished and working, however one little problem the session variables don't refresh immediatly since the session gets set when they login. So to see the effect my user needs to login and logout how could I fix this? See code below:

<form  action="includes/updateprofile.php" method="POST">

            <p class="update-title">Update profile info</p>

            <label class="popuplabel">First-name</label>
            <input name="firstname" class="popupinput" value=<?php echo $_SESSION['Firstname']; ?> />

            <label class="popuplabel">Last-name</label>
            <input name="lastname" class="popupinput" value=<?php echo $_SESSION['Lastname']; ?> />

            <label class="popuplabel">Email</label>
            <input name="email" value=<?php echo $_SESSION['Email']; ?> class="popupinput" />



            <button class="profile-edit-btn updatebutton" type="submit">Update</button>
        </form>
<?php
session_start();

require_once('../connect.php');

if(isset($_POST)){

    $firstname = mysqli_real_escape_string($connection, $_POST['firstname']);
    $lastname = mysqli_real_escape_string($connection, $_POST['lastname']);
    $email = mysqli_real_escape_string($connection, $_POST['email']);
    $id = $_SESSION['ID'];
    $newsession = $id;


    $sql = "UPDATE login SET Firstname = '".$firstname."', Lastname = '".$lastname."', Email = '".$email."' WHERE id='".$id."' ";

    $resultupdate = mysqli_query($connection, $sql);
    if($resultupdate){




        $url = "../profile.php";
        $messageok = 'User data updated!';
        echo "<script type='text/javascript'>alert('$messageok');</script>";
        echo '<script>window.location = "'.$url.'";</script>';
    }else{
        mysqli_error($resultupdate);
        $url = "../updateprofilepage.php";
        echo '<script>window.location = "'.$url.'";</script>';
    } 


}
?>
Pieter Bracke
Pieter Bracke
4,078 Points

Found the anwser myself already

simple restate the $_SESSION['Firstname'] = $firstname; $_SESSION['Lastname'] = $lastname; $_SESSION['Email'] = $email; after the escaped strings and the posted data from the user will become the session data and on relogging the data will be in the database and the session will be set as normal with the updated userdata :)