douyun3631 2017-04-06 13:58
浏览 54
已采纳

为什么我的php脚本没有动态填充html表单上的输入字段?

I am building an html form that will need to dynamically fill in the first input field with the "facility" column values in my "doctors" table. The facility column contains the names of our 7 offices. However, when I run the code below, my input field is blank and I have verified there is data in my "doctors" table. After this is working, I need to be able to dynamically fill in the second input field (which I haven't coded for in the code below because I'm stuck with the issue of first input) with the "provider" column values, also from my "doctors" table. The "provider" column contains all the provider names in our practice. However, the providers should be filtered, so that only the providers at the facility from the first input field is showing.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <?php
     $link = mysqli_connect("localhost","USERNAME","PASSWORD");
     mysqli_select_db($link,"DB");
  ?>
  <head>
    <title> Untitled Doc</title>
    <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
  </head>
  <body>
    <form name "form1" action="" method="post">
      <select>
        <?php
        $res=mysqli_query($link,"select facility from doctors");
        while($row=mysqli_fetch_array($res)){?>
          <option> <?php echo $row ["facility"]; ?></option>
        <?php }?>
      </select>
    </form>
  </body>
</html>
  • 写回答

4条回答 默认 最新

  • dtuzjzs3853 2017-04-06 14:49
    关注

    It is important that you check if the connection have been established, as I have copied your code as it is and use it on my side and was working fine, therefore made me suspect that the problem might be connection related. also check how sensitive your server is maybe your server see this as an error : $row ["facility"] that space might be the problem as well, but it didn't on my side.

    Check your server error log and also enable error reporting at the top of your page add

    <?php 
    ini_set('display_errors', 1); 
    error_reporting(E_ALL);?>
    

    That will enable error reporting, but use that on local server only

    Then on live site send them to error log

    error_reporting(E_ALL);
    ini_set('display_errors',0);
    ini_set('log_errors',1);
    

    also to get the mysqli errors, before your connection

    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

    Important to check if your query does indeed return results before trying to display them.

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
        <html>
        <?php
         $link = mysqli_connect("localhost", "root", "", "DB");
            if (mysqli_connect_errno()) {
                printf("Connect failed: %s
    ", mysqli_connect_error());
                exit();
            }
        ?>
        <head>
        <title> Untitled Doc</title>
        <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
        </head>
        <body>
        <form name ="form1" action="" method="post">
        <?php
           $query = "SELECT facility FROM doctors";
    
            if ($res = mysqli_query($link, $query)) {
                echo "<select name=\"myselect\">";
                while ($row = mysqli_fetch_assoc($res)) {
            ?>
    
            <option value="<?php echo $row['facility'];?>"><?php echo $row['facility'];?></option>
               <?php
                }
    
                echo "</select>";
                mysqli_free_result($res);
            } else {
    
                printf("Error : %s
    ", mysqli_error($link));
            }
    
            /* close connection */
            mysqli_close($link);
            ?>
    
    
    
        </form>
        </body>
        </html>
    

    NB: For your own benefit, if you haven't used prepared statements, would suggest that you learn them as well, though they are not needed in this case

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效