You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Select选项不可见:从数据库填充下拉框后选项内容无法显示

解决下拉框选项内容不可见问题

问题原因

你的代码存在两个关键问题:

  • PHP变量未输出:在<option>标签里,你只写了<?php $row['idattributevalue']; ?>和<?php $row['attributevalueEN']; ?>,但没有用echo将变量内容输出到HTML中,导致浏览器接收的是空的选项值和文本。
  • 多余的闭合标签:代码末尾多了一个</select>,破坏了HTML结构,可能影响浏览器正常渲染。

修复后的完整代码

<!doctype html>
<html lang="en">
<head>
</head>
<body>
  <?php 
    try {
      $conn = new PDO('sqlite:db/Ciupercomania.db');
      $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $stmt = $conn->prepare("SELECT * FROM attributes WHERE idattribute='10' ORDER by attributevalueEN ASC");
      $stmt->execute();
      $data = $stmt->fetchAll(PDO::FETCH_ASSOC);
   ?>
    <select id="mushtype" name="mushtype" >
    <option value="*" selected>Mushroom types</option>
    <?php foreach($data as $row): ?>
        <option value="<?php echo $row['idattributevalue']; ?>"><?php echo $row['attributevalueEN']; ?></option>
    <?php endforeach; ?>
    </select>
      <?php 
        } catch(PDOException $e) {
        echo "Error: " . $e->getMessage();
        }
        $conn = null;
      ?>
</body>
</html>

关键修改点

  • 在输出数据库字段值的PHP代码块中添加echo,确保变量内容被输出到HTML:
    • 原代码:<?php $row['idattributevalue']; ?> → 修改为:<?php echo $row['idattributevalue']; ?>
    • 原代码:<?php $row['attributevalueEN']; ?> → 修改为:<?php echo $row['attributevalueEN']; ?>
  • 删除代码末尾多余的</select>标签,保证HTML结构正确。

内容的提问来源于stack exchange,提问作者Szabolcs Horvath

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 12:15:38