PDA

View Full Version : Edit User Info (mysql) through a page


HugoCarvalho
07-10-06, 09:29 AM
Hi.

I have made before, a table for my classmates, with id,name,age,dob,location,website,email,...
and now, i want to make a page, where i can edit the profiles...
something like, appears a textbox with the actual content, and edit , submit and then it changes.

how i can do that?

Nico
07-12-06, 05:31 AM
Use the same form you're using for inserting a new user. At the top of this page get the userdata.


$query = mysql_query("SELECT * FROM users WHERE userid = ". intval($_GET['userid']));

if (mysql_num_rows($query) == 1)
{
$userinfo = mysql_fetch_array($query);
}
else
{
echo 'User not found';
}


Now $userinfo holds all data for the selected user. Examples:

$userinfo['username'] // Username
$userinfo['password'] // Password
// ...


Depending on how the fields are called in your table.

Now in the form, change the input fields like this.


<input type="text" name="username" value="<?php echo $userinfo['username']; ?>" />


Do this with all fields, so they will hold the current values. Then add a hidden field with the userid.

<input type="hidded" name="userid" value="<?php echo $userinfo['userid']; ?>" />


We need this to update the correct user. Now add something like this to the top of your page.



if (isset($_POST))
{
$query = "UPDATE users SET username = '". mysql_real_escape_string($_POST['username']) ."',
password = '". mysql_real_escape_string($_POST['password']) ."',
age = '". mysql_real_escape_string($_POST['age']) ."'
WHERE userid = '". intval($_POST['userid']) ."'";
mysql_query($query) OR die( mysql_error() );
}



http://dev.mysql.com/doc/refman/5.0/en/update.html