View Full Version : MySQL to a Form and Back Again

04-25-2007, 06:43 PM
Okay, so I figured out the login script that I was working on-- except for a few small problems which I can't fix until this one is solved. . . So, here it is!

I need to first display all of the usernames as hyperlinks. When one is clicked, I need to pull their info out of the MySQL database and display it in a from (i.e. the username in a text box) so that the info can be edited and then "saved," if you will. How do I do this? The obvious part is

print '<form>';
print "<input type='text' value='$username'>"; etc.
print '<form>';

But could someone give me an example of how I might do the rest? (I don't need the entire code-- at least I hope not :) )


04-25-2007, 07:17 PM
if(!is_numeric($id = (@$_GET['id'] or $_POST['id'])))
// throw an error and die

$users_table = 'users';
$acceptable_rows = array('username', 'email');
if(isset($_POST['username'])) {
foreach($acceptable_rows as $row => $value)
mysql_query(sprintf('update &#37;s where id=%d set \'%s\'=\'%s\'', $users_table, $id, $row, $value));
// output a success message and die

$user = mysql_fetch_array(mysql_query(sprintf('select * from %s where id=%d', $users_table, $id)));
<form action="<?php echo($_SERVER['PHP_SELF']); ?>" method="post">
<th>Email Address</th>
<?php echo($user['id']); ?>
<input type="hidden" name="id" value="<?php echo($user['id']); ?>">
<input type="text" name="username" value="<?php echo($user['username']); ?>">
<input type="text" name="email" value="<?php echo($user['email']); ?>">
<input type="submit" value="OK">