Forum Moderators: coopster

Message Too Old, No Replies

Updating & Select Box Selected

php updating mysql database with select boxes

         

bucca

11:31 pm on Sep 22, 2008 (gmt 0)

10+ Year Member



I have a PHP updating question when working with a MySQL database. Below is the code that I use to ADD a value to the "type" field in the database. What I don't understand is how to properly pull the selected value back out of the database so that it is SELECTED, when I am preforming an UPDATE.

<!-- ADDING -->
<select id='type'>
<option value='r' SELECTED>Restaurant/Bar</option>
<option value='s'>Store/Retailer</option>
<option value='d'>Distributor</option>
</select>

I found this example code, but I can't seem to figure out why it is not working for me:

<!-- UPDATING -->
<?php
while ($row = mysql_fetch_assoc($result))
{
echo '<select name="type" id="type">';
echo '<option'.($row['type']=="r"? ' selected' : '').'>Restaurant/Bar</option>';
echo '<option'.($row['type']=="s"? ' selected' : '').'>Store/Retailer</option>';
echo '<option'.($row['type']=="d"? ' selected' : '').'>Distributor</option>';
echo '</select>';
}
?>

Thank you in advance from a newb who is trying to learn.

Brian

bucca

11:51 pm on Sep 22, 2008 (gmt 0)

10+ Year Member



Here is the solution.... I finally figured it out:-)

<?php
echo '<select name="type" id="type">';
echo '<option value="r"'.($row['type']=="r"? ' selected' : '').'>Restaurant/Bar</option>';
echo '<option value="s"'.($row['type']=="s"? ' selected' : '').'>Store/Retailer</option>';
echo '<option value="d"'.($row['type']=="d"? ' selected' : '').'>Distributor</option>';
echo '</select>';
?>