954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Update SESSION variables when form is submitted

I have a form in account.php that is prefilled with the user's current info but can be changed and then when you submit, it updates the database. However, when returned to the page after the update, the prefilled text is not updated. It only updates if you re-log in. At this point I have only messed with updating the username.

ACCOUNT.PHP

<div id="main_middle"> 
<h1>&nbsp;
</h1>
<h1>MANAGE YOUR ACCOUNT INFORMATION</h1>
<p>&nbsp;</p>
username :
<form method="post" action="update_check.php">
  <input type="text" name="username" value="<?php echo "".$_SESSION['username'].""; ?>" size="40" />
  <input name="update data" type="submit" id="update data2" value="save changes"/>
</form>

<p>&nbsp;</p>
email:
<form method="post" action="update_check.php">
<input name="email" type="text" id="email" value="<?php echo "".$_SESSION['email'].""; ?>" size="40" />
<input name="update data" type="submit" id="update data" value="save changes"/>



<input name="reset" type="reset" id="reset" value="reset form"/>
</form>

</div>

I have set up the username session variable to update if the database updates, however, it does not seem to be working correctly. Any suggestions?

UPDATE_CHECK.PHP

<?php

include("db_config.php");

$tbl_name="usernames"; // Table name

// username and password sent from form 
$username=$_POST['username'];

$username = stripslashes($username);
$username = mysql_real_escape_string($username);

$username = $_POST['username'];
$checkformembers = mysql_query("SELECT * FROM email_confirm WHERE username = '$username'");
if(mysql_num_rows($checkformembers) != 0){
header("location:sign_up_usererror.php");
die();
}

$username = $_POST['username'];
$checkformembers = mysql_query("SELECT * FROM usernames WHERE username = '$username'");
if(mysql_num_rows($checkformembers) != 0){
header("location:sign_up_usererror.php");
die();
}

$result = mysql_query("UPDATE usernames SET username = '$username'");

if($result) {
$_SESSION['username'] = $_POST['username'];
header("location:account.php");
die();
}

?>
megachip04
Light Poster
34 posts since Jul 2011
Reputation Points: 10
Solved Threads: 1
 

right after your beginning php tag, line 2 call session start.

session_start();
ddymacek
Posting Whiz
317 posts since Jun 2010
Reputation Points: 36
Solved Threads: 64
 

wow...:/

thanks for the swift reply!

would it be ok for me to just insert it at the end of the db_config.php that I included in the beginning?

megachip04
Light Poster
34 posts since Jul 2011
Reputation Points: 10
Solved Threads: 1
 

@megachip04 : you must add session_start(); in the first line after php tag. It must be error like this if you put session_start(); after any code.

Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at E:\xampp\htdocs\daniweb\session.php:1) in E:\xampp\htdocs\daniweb\session.php on line 2
leakbali
Light Poster
41 posts since Jul 2011
Reputation Points: 10
Solved Threads: 8
 

it should be the first thing on the page after your beginning php tag.

<?php
session_start();
// rest of your code.

otherwise, as leakbali pointed out, if you have output to the page prior to session_start call, you may get a php warning as described above.
you could include it in your db_config code. just put it at the very top of the page.

ddymacek
Posting Whiz
317 posts since Jun 2010
Reputation Points: 36
Solved Threads: 64
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: