How do I bring forward the SELECTED option in PHP from MySQL?
Posted
by Derek
on Stack Overflow
See other posts from Stack Overflow
or by Derek
Published on 2010-03-18T23:13:49Z
Indexed on
2010/03/18
23:41 UTC
Read the original article
Hit count: 287
Hi all,
In my update form, I want the fields to recall the values that are already stored. This is very simple in a text field, but for my drop down () I'm having trouble with PHP reading the already stored name of user. Here is my query and code:
$sql = "SELECT users.user_id, users.name FROM users";
$result = mysql_query($sql, $connection)
or die ("Couldn't perform query $sql <br />".mysql_error());
$row = mysql_fetch_array($result);?>
<label>Designated Person:</label> <select name="name" id="name">
<option value="<?php echo $row['user_id']?>" SELECTED><?php echo $row['name']?> - Current</option>
<?php
while($row = mysql_fetch_array($result))
{ ?> <option value="<?php echo $row['user_id']; if (isset($_POST['user_id']));?>"><?php echo $row['fullname']?></option>
<?php } ?>
The result of this displays all of the users (as required) and lets me select a user then perform the change successfully...however the 'SELECTED' is always the first one in my database and never the user that was selected when my activity was added :( !!!
© Stack Overflow or respective owner