Forum Moderators: coopster
I have a form that uses drop-down boxes to save information to a MySQL database using a php script I wrote. That works fine, but the problem comes when I am trying to edit the information in the database. I have an "edit form" that pulls the values from the database and displays the fields in the fields accordingly. However, I am having a hard time figuring out how to populate the drop-down boxes with the value from the database. Basically, I want the "option selected" tag to be the database and be able to still have the rest of the options to select from. Does this make sense?
Thanks!
Steve
echo "<option value="{$date}">{$news}</option>";
}
?>
<option value=""></option>
</select>
is this what you're looking for?
electricocean
First, I must say I appreciate your help with this but that's not what I'm trying to do. I re-read my post and it seems confusing so let me try to explain further. I have a database that keeps information about various equipment in my organization. To populate the database, I have a form that has various text fields and drop-down boxes. I then have a PHP script that takes those values and puts them into a database. That works great. However, the problem comes when I need to edit the data that was put into the database in that fashion. I have an edit form that pulls the values from the fields in the database and prints them as values into the text fields on the edit form but I need a way to present the values that were inserted into the database via a drop-down box in a drop-down box on the edit form. For instance, lets say I have a field in the database with a value of "Television" which was put in there by a drop-down box value with many other options like "VCR", "DVD Player", etc. I need to be able to show that "Television" is the option selected in the drop-down box on the edit form so I can see what the value is but I must be able to change it so that's why I need to have the drop down box with all of the other options.
Is this clearer?
Thanks!
Steve
It sounds like you just need to extract both the name and the value from the database, and when generating the drop down box, do it like this:
echo "<option value=\"whatever\">" . $the_item " = " . $the_value . "</option>"; That way it displays the value alongside the item name.
Unless I still do not know exactly what you mean :)
$query = mysql_query("SELECT * FROM table") or die(mysql_error());while ($row = mysql_fetch_assoc($query))
{
echo '<select name="dropdown">';
echo '<option'.($row['name']=="TV"? ' selected' : '').'>TV</option>';
echo '<option'.($row['name']=="Video"? ' selected' : '').'>Video</option>';
echo '<option'.($row['name']=="Car"? ' selected' : '').'>Car</option>';
echo '<option'.($row['name']=="House"? ' selected' : '').'>House</option>';
echo '</select>';
}
Try that. Or are you just pulling a single entry? In which case this would do:
$query = mysql_query("SELECT * FROM table WHERE id = 'id'") or die(mysql_error());
$row = mysql_fetch_object($query);echo '<select name="dropdown">';
echo '<option'.($row->name=="TV"? ' selected' : '').'>TV</option>';
echo '<option'.($row->name=="Video"? ' selected' : '').'>Video</option>';
echo '<option'.($row->name=="Car"? ' selected' : '').'>Car</option>';
echo '<option'.($row->name=="House"? ' selected' : '').'>House</option>';
echo '</select>';
Best if you post your code.
dc
Steve
It will indeed work ok for that, you would need a couple of loops and you would be good to go. You might also want to think of storing your options in array too. Easier to add another option to an array than keep going through your pages and editing HTML.
$info = array('TV','Video','Car', 'House');
$query = mysql_query("SELECT * FROM table") or die(mysql_error());
while ($row = mysql_fetch_assoc($query))
{echo '<select name="dropdown">';
foreach ($info as $data)
{
echo '<option'.($row['name']==$data? ' selected' : '').'>'.$data.'</option>';
}echo '</select>';
}
Good luck.
:)
Or you could have another table that consists of a list of all of your options and then cross conect each item with the id#.
I don't know if there is away to call all the posibilities of an enum in a database. seems like their should be.