PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
mekha
Forum Contributor
Posts: 112 Joined: Sat Mar 31, 2012 6:50 am
Post
by mekha » Mon Apr 16, 2012 6:38 am
Problem with refreshing session
hi..
i have updated the database...
in form (post)....
now the database is updated,but the form is not updated..i must log out and log in again to see the updates!..
what code i need to put into updated.php , to back to member profile with the updated informations? here is updated.php code:
Code: Select all
<?php
require_once("functions.php");
$age = $_POST['age'];
$userid=$_POST['fld_id'];
if(trim($age == ''))
die('You cannot let the age empty!</br><a href="index.php">Go Back</a>');
$result = update_info($age,$userid);
if($result == false)
die('Problem Updating The age');
else
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Title updated</title>
</head>
<body>
<?php
header("Location: index.php");
?>
</body>
</html>
Last edited by
mekha on Mon Apr 16, 2012 10:24 am, edited 1 time in total.
mekha
Forum Contributor
Posts: 112 Joined: Sat Mar 31, 2012 6:50 am
Post
by mekha » Mon Apr 16, 2012 6:39 am
this is the function.php:
Code: Select all
<?php
include "base2.php" ;
function update_info($age,$userid)
{
$query="UPDATE users SET Ronaldo='$age' WHERE UserID=".$userid;
mysql_query("SET NAMES 'utf8'");
$result=mysql_query($query);
if(!$result)
return false;
else
return true;
}
?>
base2.php is the connection to the data base
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Mon Apr 16, 2012 6:46 am
I don't see a form anywhere in the code you've posted.
mekha
Forum Contributor
Posts: 112 Joined: Sat Mar 31, 2012 6:50 am
Post
by mekha » Mon Apr 16, 2012 6:49 am
This is all the home page with the form:
Code: Select all
<?php
include "base.php";
require_once("functions.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>User Management System (Tom Cameron for NetTuts)</title>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div id="main">
<?php
if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']))
{
?>
<h1>Member Area</h1>
<p>Thanks for logging in! You are <b><?=$_SESSION['Username']?></b> and your email address is <b><?=$_SESSION['EmailAddress']?></b>.</br></br><h3>Your Age is: <?=$_SESSION['Ronaldo']?></h3></br></br>
<form method="post" action="updated.php">
<input type="hidden" name="fld_id" value="<?=$_SESSION['UserID']?>">
<table style="border:1px solid red;">
<tr>
<td>Update your age:</td>
<td> ID = <?=$_SESSION['UserID']?></td>
</tr>
<tr>
<td><input type="text" name="age" id="age" value="<?php echo $_SESSION['Ronaldo']; ?>"></td>
<td></td>
</tr>
<tr>
<td><input type="submit" value="update"></td>
<td></td>
</tr>
</table>
</form>
</br></br>
<a href="logout.php">Log out</a>
</p>
<?php
}
elseif(!empty($_POST['username']) && !empty($_POST['password']))
{
$username = mysql_real_escape_string($_POST['username']);
$password = md5(mysql_real_escape_string($_POST['password']));
$checklogin = mysql_query("SELECT * FROM users WHERE Username = '".$username."' AND Password = '".$password."'");
if(mysql_num_rows($checklogin) == 1)
{
$row = mysql_fetch_array($checklogin);
$email = $row['EmailAddress'];
$ronaldo = $row['Ronaldo'];
$userid = $row['UserID'];
$_SESSION['Username'] = $username;
$_SESSION['EmailAddress'] = $email;
$_SESSION['Ronaldo'] = $ronaldo;
$_SESSION['UserID'] = $userid;
$_SESSION['LoggedIn'] = 1;
echo "<h1>Success</h1>";
echo "<p>We are now redirecting you to the member area.</p>";
echo '<meta http-equiv="refresh" content="2;index.php"> ';
}
else
{
echo "<h1>Error</h1>";
echo "<p>Sorry, your account could not be found. Please <a href=\"index.php\">click here to try again</a>.</p>";
}
}
else
{
?>
<h1>Member Login</h1>
<p>Thanks for visiting! Please either login below, or <a href="register.php">click here to register</a>.</p>
<form method="post" action="index.php" name="loginform" id="loginform">
<fieldset>
<label for="username">Username:</label><input type="text" name="username" id="username" /><br />
<label for="password">Password:</label><input type="password" name="password" id="password" /><br />
<input type="submit" name="login" id="login" value="Login" />
</fieldset>
</form>
<?php
}
?>
</div>
</body>
</html>
mekha
Forum Contributor
Posts: 112 Joined: Sat Mar 31, 2012 6:50 am
Post
by mekha » Mon Apr 16, 2012 6:55 am
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Mon Apr 16, 2012 6:58 am
You're populating the form from session data. updated.php updates the database but never updates session info.
mekha
Forum Contributor
Posts: 112 Joined: Sat Mar 31, 2012 6:50 am
Post
by mekha » Mon Apr 16, 2012 7:10 am
can u help me plz in how to update session info ?
mekha
Forum Contributor
Posts: 112 Joined: Sat Mar 31, 2012 6:50 am
Post
by mekha » Mon Apr 16, 2012 9:34 am
this is my problem:
http://topline.me/membership
try this user:
user:ronaldo
password:samba555
...
try to update age....
its be updated in the sql only...but in Form not :\
Celauran
Moderator
Posts: 6427 Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada
Post
by Celauran » Mon Apr 16, 2012 9:43 am
You're populating the form value from session data
Code: Select all
<td><input type="text" name="age" id="age" value="<?php echo $_SESSION['Ronaldo']; ?>"></td>
Once the form has been submitted, you're going here:
Code: Select all
<?php
require_once("functions.php");
$age = $_POST['age'];
$userid=$_POST['fld_id'];
if(trim($age == ''))
die('You cannot let the age empty!</br><a href="index.php">Go Back</a>');
$result = update_info($age,$userid); // This updates the database (with no escaping!)
if($result == false)
die('Problem Updating The age');
There's no session_start() on this page, and no $_SESSION['Ronaldo'] = whatever;
mekha
Forum Contributor
Posts: 112 Joined: Sat Mar 31, 2012 6:50 am
Post
by mekha » Mon Apr 16, 2012 10:22 am
Oh man...thats what i need hhh :\ ... Great...my problem solved...thank u very much man!
this is the new code:
Code: Select all
<?php
session_start(); // i've added this
require_once("functions.php");
$age = $_POST['age'];
$userid=$_POST['fld_id'];
if(trim($age == ''))
die('You cannot let the age empty!</br><a href="index.php">Go Back</a>');
$result = update_info($age,$userid);
if($result == false)
die('Problem Updating The age');
else
$_SESSION['Ronaldo'] = $age; // i've added this
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="1; url=index.php" />
<title>Title updated</title>
</head>
<body>
</body>
</html>