Dropdown box from MySQL

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
DanHorniblow
Forum Newbie
Posts: 3
Joined: Wed Feb 15, 2012 6:50 pm

Dropdown box from MySQL

Post by DanHorniblow »

Hi, I am trying to populate a drop down menu from MySQL, which lists all of the users in a database that haven't been approved, I then want a button next to this drop down which would then approve the user that is selected in the drop down.

The drop down is being populated with the correct information but I can't seem to figure out how to add the button next to the drop down and make it approve the selected user. This is what I have so far:

Code: Select all

$result = mysql_query("SELECT * FROM user WHERE approved = 'no'");
							
echo "<select name='approval'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['username'] . "'>" . $row['username'] . "</option>";
}
echo "</select>";
Thanks in advanced
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Dropdown box from MySQL

Post by Celauran »

What have you tried? What errors are you encountering? A regular old submit button should work just fine.
DanHorniblow
Forum Newbie
Posts: 3
Joined: Wed Feb 15, 2012 6:50 pm

Re: Dropdown box from MySQL

Post by DanHorniblow »

Hi, Thanks for your reply

I am not sure what code I need behind a submit button in order to change the "approved" filed to "yes" in the db for the user selected.

Sorry I am quite new to php.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Dropdown box from MySQL

Post by Celauran »

Untested, but something like this:

Code: Select all

<?php
if (!empty($_POST))
{
    $id = intval($_POST['approval']);
    $query = "UPDATE user SET approved = 'yes' WHERE id = $id";
    mysql_query($query);
}

$query = "SELECT id, username FROM user WHERE approved = 'no'";
$result = mysql_query($query);

?>

<form action="" method="post">
    <select name="approval">
        <?php while ($row = mysql_fetch_assoc($result)): ?>
        <option value="<?php echo $row['id']; ?>"><?php echo $row['username']; ?></option>
        <?php endwhile; ?>
    </select>
    <input type="submit" value="Approve" />
</form>
DanHorniblow
Forum Newbie
Posts: 3
Joined: Wed Feb 15, 2012 6:50 pm

Re: Dropdown box from MySQL

Post by DanHorniblow »

Thanks very much, your code works perfectly.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Dropdown box from MySQL

Post by Celauran »

And alternate approach you may want to consider is using a series of checkboxes so you can approve multiple members at once.
Post Reply