millercj Posted April 12, 2008 Share Posted April 12, 2008 I'm working with a login portal and when the user logs in a bunch of session variables are set. Once they are logged in there is an option for a form to update their information (which is saved in a database). The form fields load with the session variables as the values for the fields: <input class="text" type="text" size="15" name="lname" id="lname" value="<?PHP echo $_SESSION['lname']?>"/> When the user submits the form, it looks at the database and says this: <?php session_start(); require_once "action_connect.php"; $newfname=$_POST['fname']; $newlname=$_POST['lname']; $newemail=$_POST['email']; $fname=$_SESSION['fname']; $lname=$_SESSION['lname']; $email=$_SESSION['email']; $sql = "SELECT * FROM users WHERE first_name='$fname' AND last_name='$lname' AND email='$email'"; if ($r = mysql_query ($sql)) { $row = mysql_fetch_array ($r); $num = mysql_num_rows ($r); if ($num > 0) //If the username and password are in a row { if($newfname != $row['first_name']) { mysql_query("UPDATE users SET first_name = '$newfname' WHERE first_name='$fname' AND last_name='$lname' AND email='$email'"); $_SESSION['fname'] = $row['first_name']; } ?> and so on for the fields The database is successfully changed but the session variable has not changed. When the page reloads the original value (set when the user logs in) load in the form fields. They will obviously change when the user logs out and logs back in, but how to I change/reset session variables during the session? Link to comment https://forums.phpfreaks.com/topic/100750-changing-session-variables/ Share on other sites More sharing options...
marcus Posted April 12, 2008 Share Posted April 12, 2008 just redefine it $_SESSION['name'] = "larry"; Link to comment https://forums.phpfreaks.com/topic/100750-changing-session-variables/#findComment-515318 Share on other sites More sharing options...
millercj Posted April 12, 2008 Author Share Posted April 12, 2008 isn't that what this does: $_SESSION['fname'] = $row['first_name']; Link to comment https://forums.phpfreaks.com/topic/100750-changing-session-variables/#findComment-515319 Share on other sites More sharing options...
Gamic Posted April 12, 2008 Share Posted April 12, 2008 <?php session_start(); require_once "action_connect.php"; $newfname=$_POST['fname']; $newlname=$_POST['lname']; $newemail=$_POST['email']; $fname=$_SESSION['fname']; $lname=$_SESSION['lname']; $email=$_SESSION['email']; $sql = "SELECT * FROM users WHERE first_name='$fname' AND last_name='$lname' AND email='$email'"; if ($r = mysql_query ($sql)) { /*line 16*/$row = mysql_fetch_array ($r); $num = mysql_num_rows ($r); if ($num > 0) //If the username and password are in a row { if($newfname != $row['first_name']) { mysql_query("UPDATE users SET first_name = '$newfname' WHERE first_name='$fname' AND last_name='$lname' AND email='$email'"); $_SESSION['fname'] = $row['first_name'];//$row came from line 16 above and so is the old data } ?> Instead do this: <?php session_start(); require_once "action_connect.php"; /*from form*/ $newfname=$_POST['fname']; $newlname=$_POST['lname']; $newemail=$_POST['email']; $fname=$_SESSION['fname']; $lname=$_SESSION['lname']; $email=$_SESSION['email']; $sql = "SELECT * FROM users WHERE first_name='$fname' AND last_name='$lname' AND email='$email'"; if ($r = mysql_query ($sql)) { $row = mysql_fetch_array ($r); $num = mysql_num_rows ($r); if ($num > 0) //If the username and password are in a row { if($newfname != $row['first_name']) { $result=mysql_query("UPDATE users SET first_name = '$newfname' WHERE first_name='$fname' AND last_name='$lname' AND email='$email'"); $_SESSION['fname'] = $newfname;//instead use newfname as that comes from the form } ?> Link to comment https://forums.phpfreaks.com/topic/100750-changing-session-variables/#findComment-515364 Share on other sites More sharing options...
millercj Posted April 12, 2008 Author Share Posted April 12, 2008 Nope, still nothing. This is my full code...The changes Gamic suggested are applied to the fname conditionals the other two are still the original <?php session_start(); require_once "action_connect.php"; $newfname=$_POST['fname']; $newlname=$_POST['lname']; $newemail=$_POST['email']; $user=$_SESSION['username']; $fname=$_SESSION['fname']; $lname=$_SESSION['lname']; $email=$_SESSION['email']; $change=0; $sql = "SELECT * FROM users WHERE first_name='$fname' AND last_name='$lname' AND email='$email'"; if ($r = mysql_query ($sql)) { $row = mysql_fetch_array ($r); $num = mysql_num_rows ($r); if ($num > 0) { if($newfname != $row['first_name']) { $results=mysql_query("UPDATE users SET first_name = '$newfname' WHERE first_name='$fname' AND last_name='$lname' AND email='$email'"); $_SESSION['fname'] = $newfname; $change=1; } if($newlname != $row['last_name']) { mysql_query("UPDATE users SET last_name = '$newlname' WHERE first_name='$fname' AND last_name='$lname' AND email='$email'"); $_SESSION['lname'] = $row['last_name']; $change=1; } if($newemail != $row['email']) { mysql_query("UPDATE users SET email = '$newemail' WHERE first_name='$fname' AND last_name='$lname' AND email='$email'"); $_SESSION['email'] = $row['email']; $change=1; $sqle = "SELECT * FROM recover WHERE username='$user'"; if ($re = mysql_query ($sqle)) { $rowe = mysql_fetch_array ($re); $nume = mysql_num_rows ($re); if ($nume > 0) { mysql_query("UPDATE recover SET email = '$newemail' WHERE username='$user'"); $change=1; } } } if($change == 1) {echo 'Update Successful!';} if($newemail == $row['email'] && $newlname == $row['last_name'] &&$newfname == $row['first_name']) {echo 'No changes have been made';} } else{echo 'ERROR: If you have recently made a change<br/>logout, login, and retry';} } ?> Link to comment https://forums.phpfreaks.com/topic/100750-changing-session-variables/#findComment-515494 Share on other sites More sharing options...
millercj Posted April 12, 2008 Author Share Posted April 12, 2008 Any one have any clue? Link to comment https://forums.phpfreaks.com/topic/100750-changing-session-variables/#findComment-515739 Share on other sites More sharing options...
Gamic Posted April 13, 2008 Share Posted April 13, 2008 How do you travel around the site after you submit the form and the update happens? Is there a link that you follow to get back to the form, is the form included using php, or do you use the browser's back button to travel back to the form? Link to comment https://forums.phpfreaks.com/topic/100750-changing-session-variables/#findComment-515785 Share on other sites More sharing options...
millercj Posted April 13, 2008 Author Share Posted April 13, 2008 it's actually ajax so the form never reloads. But if i manually reload the page, or navigate away from the page and come back it still doesn't change Link to comment https://forums.phpfreaks.com/topic/100750-changing-session-variables/#findComment-515998 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.