一尘不染

从数据库表填充选择下拉列表

mysql

我有一个表格(“场地”),其中存储了志愿者可以工作的所有可能场所,每个志愿者被分配为每个场所工作一个。

我想从场所表中创建一个选择下拉列表。

现在,我可以显示分配给每个志愿者的地点,但是我希望它显示下拉框,并且已经在列表中选择了地点。

<form action="upd.php?id=7">
<select name="venue_id">
<?php //some sort of loop goes here
print '<option value="'.$row['venue_id'].'">'.$row['venue_name'].'</option>';
//end loop here ?>
</select>
<input type="submit" value="submit" name="submit">
</form>

例如,将ID为7的志愿者分配给了场地编号4

<form action="upd.php?id=7">
<select name="venue_id">
    <option value="1">Bagpipe Competition</option>
    <option value="2">Band Assistance</option>
    <option value="3">Beer/Wine Pouring</option>
    <option value="4" selected>Brochure Distribution</option>
    <option value="5">Childrens Area</option>
    <option value="6">Cleanup</option>
    <option value="7">Cultural Center Display</option>
    <option value="8">Festival Merch</option>
</select>
<input type="submit" value="submit" name="submit">
</form>

Brochure Distribution option will already be selected when it displays the drop down list, because in the volunteers_2009 table, column venue_id is 4.

我知道它将采用for或while循环的形式从场地表中拉出场地列表

我的查询是:

$query = "SELECT volunteers_2009.id, volunteers_2009.comments, volunteers_2009.choice1, volunteers_2009.choice2, volunteers_2009.choice3, volunteers_2009.lname, volunteers_2009.fname, volunteers_2009.venue_id, venues.venue_name FROM volunteers_2009 AS volunteers_2009 LEFT OUTER JOIN venues ON (volunteers_2009.venue_id = venues.id) ORDER by $order $sort";

如何填充选择下拉框与场馆( volunteers_2009.venue_idvenues.id 从场地表),并已预先在列表中选择场地?


阅读 292

收藏
2020-05-17

共1个答案

一尘不染

$query = "SELECT volunteers_2009.id, volunteers_2009.comments, volunteers_2009.choice1, volunteers_2009.choice2, volunteers_2009.choice3, volunteers_2009.lname, volunteers_2009.fname, volunteers_2009.venue_id, venues.venue_name FROM volunteers_2009 AS volunteers_2009 LEFT OUTER JOIN venues ON (volunteers_2009.venue_id = venues.id) ORDER by $order $sort";

$res = mysql_query($query);
echo "<select name = 'venue'>";
while (($row = mysql_fetch_row($res)) != null)
{
    echo "<option value = '{$row['venue_id']}'";
    if ($selected_venue_id == $row['venue_id'])
        echo "selected = 'selected'";
    echo ">{$row['venue_name']}</option>";
}
echo "</select>";

:)

2020-05-17