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条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?