Jump to content

Changing Session Variables


millercj

Recommended Posts

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

<?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
			}
?>

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';}
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.