drop box selection from db

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nite4000
Forum Contributor
Posts: 209
Joined: Sun Apr 12, 2009 11:31 am

drop box selection from db

Post 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
Last edited by Benjamin on Mon Jun 08, 2009 2:43 pm, edited 1 time in total.
Reason: Added [code=php] tags.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: drop box selection from db

Post 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.
Post Reply