Hello.
I have a site that users log in to/update their personal information. How do I make it so that when they click on "Edit personal information" a form comes up with their information shown - and all they have to do is change it there and click update and it's done? Maybe there they can also choose to delete themselves from the database? Any ideas/code/direction you can send me?
Thanks!
need help with form posting/updating to database
Moderator: General Moderators
Its pretty easy. All you have to do for the edit info page is:
1. Retrieve all the info about a particular user from the database.
2. Make a form with an input box for each field that can be edited.
3. On submit, query the database with the new info.
Example:
Pretend that that a user just registered and his username and password were just recorded in the database.
ID: 1
Username: Bart
Password: Simpson
The info was stored in a table called members with 3 columns (ID, username and password). The code for the edit page would be:
This is just an example, its not very secure but it should help you with what you want to do.
1. Retrieve all the info about a particular user from the database.
2. Make a form with an input box for each field that can be edited.
3. On submit, query the database with the new info.
Example:
Pretend that that a user just registered and his username and password were just recorded in the database.
ID: 1
Username: Bart
Password: Simpson
The info was stored in a table called members with 3 columns (ID, username and password). The code for the edit page would be:
Code: Select all
<?php
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM members WHERE id='$id'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$username=mysql_result($result,$i,"username");
$password=mysql_result($result,$i,"password");
++$i;
}
?>
<form action="<? echo $PHP_SELF; ?>">
Username: <input type="text" name="username" value="<? echo "$username"; ?>"><br>
Password: <input type="password" name="password" value="<? echo "$password"; ?>">
<BR><BR>
<input type="hidden" name="action" value="submit">
<input type="Submit" value="Update">
</form>
<?php
if ($action == "submit")
{
mysql_connect(localhost,$username,$password);
$query="UPDATE members SET username='$username' password='$password WHERE id='$id'";
@mysql_select_db($database) or die( "Unable to select database");
mysql_query($query);
mysql_close();
}
?>