views:

159

answers:

2

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 :( !!!

+1  A: 

Hmm... i think your code is not complete. Ex the last option got an if-statement that does nothing...and you should not put selected on two diffrent options in the same select element.

The first option is always default selected if no-one else is, and.... for xhtml try using selected="selected" instead of SELECTED

Also, you are trying to echo $row['fullname'], but your query is fetching column 'name'

<?php
$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']?>"><?php echo $row['name'] ?> - Current</option>
<?php    
while($row = mysql_fetch_array($result)){ ?>
   <option 
      value="<?php echo $row['user_id'] ?>"
      <?php echo (isset($_POST['name']) && $_POST['name'] == $row['user_id']) ? ' selected="selected"' : '');?>
><?php echo $row['name']?></option>
<?php } ?>
PHP_Jedi
+1  A: 

What you're doing now is:

  • Selecting the very first result row and printing it out as an option with SELECTED hardcoded in;
  • Looping thru the rest of the results row outputting options; you aren't checking for currently selected value anywhere, but you have an empty if() there for no purpose;
  • I can't see anywhere where the currently stored name is held.

Try this (assuming $curname is the currently selected name):

$sql = "SELECT users.user_id, users.name FROM users";
        $result = mysql_query($sql, $connection)
        or die ("Couldn't perform query $sql <br />".mysql_error());
?>

<label for="name">Designated Person:</label>
    <select name="name" id="name">

 <?php    
      while($row = mysql_fetch_array($result))
{ ?>                        
    <option value="<?php echo $row['user_id'] ?>" <?php if ($curname == $row['name']) echo ' SELECTED'; ?>><?php echo $row['fullname']?></option>
<?php } ?>
djn