doulu7258 2014-11-04 15:26
浏览 23
已采纳

php填充数据库表中的选项框

I am writing code to register a new project via a html form. I want to be able to click a dropdown box which pulls the values from a table on the database.

At the moment a dropdown box displays but with no values.

PLEASE NOTE: I am a learning the basics so apologies if this is a simple question/answer scenario.

My code is below, any help is appreciated, I connect to the database via a php include script. The table is called 'customers' and the item I want to list is 'name';-

    <?php
$result = mysql_query("SELECT customers FROM name"); 
 echo "<select name='client'>"; 
 while($row = mysql_fetch_assoc($result)) 
 { 
    echo "<option value = '".$row[name]."'>".$row[name]."</option>"; 
 }
 echo "</select>";
?>
  • 写回答

2条回答 默认 最新

  • dongnuo3749 2014-11-04 15:29
    关注

    "The table is called 'customers' and the item I want to list is 'name';" -

    Do SELECT name FROM customers instead of SELECT customers FROM name

    • Using mysql_error() to mysql_query()
      would have shown you the error that the table name does not exist.

    Plus,
    [name] are missing quotes inside them => ['name'] which are being treated as constants.

    in

    echo "<option value = '".$row[name]."'>".$row[name]."</option>";
    

    as caught and kudos to devdesign

    echo "<option value = '".$row['name']."'>".$row['name']."</option>";
    

    However, you are using a deprecated MySQL library. If you are still not getting results, then this could mean that you need to use (and should use) mysqli_ or PDO instead.

    Here are a few links on the subject:

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

报告相同问题?

悬赏问题

  • ¥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之后自动重连失效