douxiangbiao1899 2016-10-20 04:33
浏览 230
已采纳

如何从数据库中填充HTML SELECT

I'm trying to get an HTML SELECT field within a form to be populated from a database column. I can read the column fine, and can use fprint or echo to see the results. The problem is, I can't seem to get the array based on the column to appear as selections in the SELECT field. I'm able to produce a field with a pull-down selector, but the values aren't populated. How to I get the values from the Array into an HTML SELECT / OPTION field? Here's a subset of the code I'm using:

<?php 
$link = new mysqli("localhost","USER","PASSWORD", "DATABASE");
  if (mysqli_connect_errno())
   {
   printf("Connect failed: %s
", mysqli_connect_error());
  exit();
}
if (!$link->set_charset("utf8")) 
{
    printf("Error loading character set utf8: %s
", $link->error);
    exit();
}

$role_sql = "SELECT role FROM lu_role";
$role_result = mysqli_query($link, $role_sql);
$options = "";
while($row1 = mysqli_fetch_array($role_result))
{
        $options = $options."<option>$row1[1]</option>";
}

//Using the following to validate that I can get the query results in an array.
while ($row = $role_result->fetch_assoc())
{
    printf($row["role"]);
}
?>
<form action="post.php" method="post">
    <table class="table_600_reg">
      <tr>
        <td width="120">Father</td>
        <td width="200" align="left">
            <select>
                <?php echo $options;?>
            </select>               
  • 写回答

5条回答 默认 最新

  • doushouxie7064 2016-10-20 04:36
    关注

    You are missing value="" here $options = $options."<option value='VALUE_HERE'>$row1[1]</option>";

    And since you are selecting only one column it should be $row1[0] instead of $row1[1]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码