duan198727
2013-05-29 18:28
浏览 94
已采纳

下拉菜单包含来自MySQL数据库的数据作为选项

i try to get data from mysql db into html dropdown , i execute the query in PHPmyadmin and its work fine , the result is one record,and all the website is connected with theses details of MYSQL my code is :

  <?php
   mysql_connect("localhost", "root", "1212") or die("Connection Failed");
   mysql_select_db("test")or die("Connection Failed");
   $query = "SELECT department_name FROM department";
   $result = mysql_query($query);
   ?>
  <label for="department" > Department Name </label>
  <select name="departments" >

   <?php
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    ?>

   <option value="<?php echo $line['field'];?>"> <?php echo $line['field'];?> </option>

   <?php
   }
     ?>
  </select>   

the output is drop-down with one empty record , any one can help me in that ?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duandaijian1583 2013-05-29 18:31
    已采纳

    Well first of all you should print the results of the query to ensure the array structure is what you think it is.

    This would have shown you that there is no column in the result set named field as you seem to believe due to this line of code:

    <option value="<?php echo $line['field'];?>"> <?php echo $line['field'];?> </option>
    

    As to why you only get one option, my first bit of advice will probably shed some light on said issue as well.

    点赞 评论
  • dongzhi4073 2013-05-29 18:32

    First required statement: mysql_ is deprecated. mysqli_ should be used instead.

    Second ...

    Did you try $line['department_name'] instead of $line['field'] ?

    点赞 评论

相关推荐 更多相似问题