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

报告相同问题?

悬赏问题

  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退
  • ¥20 win系统的PYQT程序生成的数据如何放入云服务器阿里云window版?