使用数据库值填充下拉列表

时间:2014-07-07 13:16:28

标签: php forms drop-down-menu

我还是比较新的php所以请耐心等待。我在其他例子中看过很多次,但仍然无法解决这个问题。

我有一个添加页面,其中有一个由查询填充的下拉列表。这可以根据需要使用。我的问题现在在编辑表单上,让下拉框显示数据库字段中的值。

<select name="lvmID" id="lvmID">

<option value="">--Select--</option>
<?php
include ('../datalogin.php');
$list=mysql_query("select luchtvaartmaatschappijID, luchtvaartmaatschappij from  
tbl_luchtvaartmaatschappij WHERE inactive='0' Order by luchtvaartmaatschappij ASC");
while($row_list=mysql_fetch_assoc($list)){    ?>

<option value="<?echo $row_list['luchtvaartmaatschappijID']; ?>">
<?echo $row_list['luchtvaartmaatschappij']; ?>          
</option>

         <?
         }
         ?>
</select>

这在“添加”表单上按预期工作。但是当我进入编辑表单以更改该页面的信息时。选择下拉列表仍然填充,但我无法弄清楚如何获取它,以便存储值的数据库表中的值(而不是下拉列表中的值)不会被选中。

所以也许价值应该是&#39; 1747&#39; &#39;荷兰皇家航空公司&#39;。当我转到bijwerk.php时,我无法弄明白这一点,这个值在下拉列表中尚未被选中。

bijwerk.php - 从tbl_vluchtgegevens表中提取信息。

<body>
<?    include "datalogin.php";//database connection

$order = "select vg.*, lh.luchthavencode as vertrekluchthavencode, lh2.luchthavencode 
AS aankomstluchthavencode, lvm.luchtvaartmaatschappij AS lvmnaam, 
lvm.luchtvaartmaatschappijID, t.toestel AS toestelnaam, k.reisklass, r.reizen, 
k.reisklass, vt.vluchttype AS revenue

from tbl_vluchtgegevens vg

left join tbl_luchthaven lh
on vg.vertrekluchthaven = lh.luchthavenID

left join tbl_reizen r
on vg.reisID = r.reizenID  

left join tbl_luchthaven lh2
on vg.aankomstluchthaven = lh2.luchthavenID

left join tbl_toestel t
on vg.toestel = t.toestelID

left join tbl_klass k
on vg.reisklasse = k.klassID

left join tbl_vluchttype vt
on vg.vluchttype = vt.vluchttypeID

left join tbl_luchtvaartmaatschappij lvm
on vg.luchtvaartmaatschappij = lvm.luchtvaartmaatschappijID

WHERE gegevenID='$id'";
  $result = mysql_query($order);
  $row = mysql_fetch_array($result);
?>

<table border=1>
<tr>
<td width="646" align=center>bijwerk vluchtgegevens: <br>
ID = <? echo "$row[gegevenID]"?></td>
<td width="505" align=center>&nbsp;</td>
</tr>

<td><select name="lvmID" id="lvmID">

<option value="">--Select--</option>
<?php
include ('../datalogin.php');
$list=mysql_query("select luchtvaartmaatschappijID, luchtvaartmaatschappij from  
tbl_luchtvaartmaatschappij WHERE inactive='0' Order by luchtvaartmaatschappij ASC");
while($row_list=mysql_fetch_array($list)){    ?>

<option value="<?echo $row_list['luchtvaartmaatschappijID']; ?>" <?php echo    
($row_list['luchtvaartmaatschappijID'] == $_POST['lvmID']) ?    
'selected="selected"' : '' ?>>
<?echo $row_list['luchtvaartmaatschappij']; ?>          
</option>

     <?
     }
     ?>
</select></td>
      </table>
<?php
// close connection 
mysql_close();
?>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

因此,如果我更正确,您需要调整bijwerk.php文件以自动获得正确的选定值。您可以通过修改循环来构建下拉列表来执行此操作:

while($row_list=mysql_fetch_assoc($list)){    ?>

<option value="<?echo $row_list['luchtvaartmaatschappijID']; ?>" <?php echo ($row_list['luchtvaartmaatschappijID'] == $_POST['luchtvaartmaatschappijID']) ? 'selected="selected"' : '' ?>>
<?echo $row_list['luchtvaartmaatschappij']; ?>          
</option>

         <?
         }
         ?>

$ _POST的值是所选'luchtvaartmaatschappijID'的当前值。

这是你的意思吗?

答案 1 :(得分:0)

您应该检查非活动字段实际上是字符串还是整数。如果是整数,则不应在查询中使用引号,该引号应如下所示:

"select luchtvaartmaatschappijID, luchtvaartmaatschappij from  
tbl_luchtvaartmaatschappij WHERE inactive=0 Order by luchtvaartmaatschappij ASC"

此外,在您的HTML中,您应该从selected="selected"更改为selected

无论如何,我建议您升级到mysqli个功能,因为mysql_ functions are deprecated并且不再安全使用。在这里您可以找到有关如何升级的好建议:

  1. Updating from MYSQL to MYSQLI
  2. Upgrading to MySQLi - As easy as swapping out mysql for mysqli?