Jump to content

user profile not updating?


dyr

Recommended Posts

Hi, I recently implemented a code to display user profile information.  Well, it displays the username and password fine, but the edit function doesn't seem to be working.  I edit the information, click submit, get a success message but the username and password didn't change.

 

myprofile.php

<?php

session_start();

include('config.php');

$sql = mysql_query( "SELECT * FROM users WHERE id='".$_SESSION['id']."'" ); 

echo "<h2>Profile</h2>
<form method='post' action='editprofile.php'>
<table>";

$row = mysql_fetch_array($sql);
echo "<tr><th>Name: </th><td>".$row['username']."</td></tr>
<tr><th>Password: </th><td><input type='password' value='".$row['password']."' disabled='true' /></td></tr>";


echo "</table><br />
<input type='submit' value='edit profile' />
</form>";

?>

 

editprofile.php

<?php

include('config.php');


if(isset($_POST['btnedit'])){
$username = $_POST['username'];
$password = $_POST['password'];

$sql = mysql_query( "UPDATE users SET username='".$username."', password='".$password."' WHERE id='".$_SESSION['id']."'" );

if($sql){
echo "<script>alert('profile updated');window.location='myprofile.php'</script>";
}else{
echo "<script>alert('updating profile failed!');</script>";
}

}




$sql = mysql_query( "SELECT * FROM users WHERE id='".$_SESSION['id']."'" ); 
$row = mysql_fetch_array($sql);
echo "<h2>Edit profile</h2>
<form method='post'>
<table>
<tr><th>registered:</th><td><input type='text' name='username' value='".$row['username']."'/></td></tr>
<tr><th>password:</th><td><input type='password' name='password' value='".$row['password']."'/></td></tr>
</table><br />
<input type='submit' name='btnedit' value='update' />
</form>";

?>

Link to comment
https://forums.phpfreaks.com/topic/258208-user-profile-not-updating/
Share on other sites

You need to redeclare the session_start() function at the beginning of editprofile.php. You're trying to access $_SESSION['id'] in the UPDATE query, but since the session_start() wasn't executed, the session isn't the same as the previous page.

 

 

Personally, I always have session_start() in my config.php file so that I never forget to do it

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.