Page 1 of 1

drop box selection from db

Posted: Mon Jun 08, 2009 2:32 pm
by nite4000
I am trying to make a drop box list the choices i have in a table which is "Active" and "Suspended" which is in the acc_status table now in the Users table i have a field "status" which is where the choice is save but i have been unable to get it to list and update the users table.



this is what i have

Code: Select all

 
<select name="acctstatus" id="acctstatus">
       <?php
    $res = mysql_query("SELECT * FROM acc_status") or die("<option>Unable to get departments!</option>");
    while($acc = mysql_fetch_array($res, MYSQL_ASSOC)) { ?>
      <option value="<?php echo $acc['id']; ?>" <?php if(intval($edit['id']) == intval($acc['id'])) { echo 'selected="selected"'; } ?>><?php echo $acc['level']; ?></option>
    <?php } ?>
        </select>
 

the edit['id'] is selecting from the users but dont think i am selecting right

Code: Select all

 
  $r = mysql_query("SELECT * FROM Users WHERE status=$acc['leve'] LIMIT 1 ") or die(mysql_error());
$edit = mysql_fetch_array($r, MYSQL_ASSOC);
@mysql_free_result($r);
 


Any help you can give me would be helpful


Thanks

Re: drop box selection from db

Posted: Mon Jun 08, 2009 10:36 pm
by califdon
Personally, I'd code it like this:

Code: Select all

<select name="acctstatus" id="acctstatus">
<?php
$res = mysql_query("SELECT * FROM acc_status") or die(mysql_error());
while($acc = mysql_fetch_assoc($res, MYSQL_ASSOC)) {
   extract($acc);
   echo "<option value='$id'";
   if(intval($edit['id']) == intval($id)) echo " selected";
   echo "> $level</option>";
}
?>
</select>
In your second query, is `status` a numeric field? If not, you need some quotes.