PDA

View Full Version : MySQL to a Form and Back Again



Jas
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 :) )

Thanks!

Twey
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">
<table>
<thead>
<tr>
<th>ID</th>
<th>Username</th>
<th>Email Address</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<?php echo($user['id']); ?>
<input type="hidden" name="id" value="<?php echo($user['id']); ?>">
</td>
<td>
<input type="text" name="username" value="<?php echo($user['username']); ?>">
</td>
<td>
<input type="text" name="email" value="<?php echo($user['email']); ?>">
</td>
<td>
<input type="submit" value="OK">
</td>
</tr>
</tbody>
</table>
</form>