从 MySQL 数据库中获取数据到 html 下拉列表 [英] Fetching data from MySQL database to html dropdown list

查看:45
本文介绍了从 MySQL 数据库中获取数据到 html 下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 html 表单的网站,在此表单中,我有一个下拉列表,其中包含在公司工作的代理列表,我想从 MySQL 数据库中获取数据到此下拉列表,因此当您添加新代理时,他的名称将作为选项出现在下拉列表中.

I have a web site that contains a html form, in this form i have a dropdownlist with list of agents that works in the company, i want to fetch data from MySQL database to this dropdownlist so when you add a new agent his name will appear as a option in the drop down list .

你能帮我编写这个php代码吗,谢谢

Can you help me coding this php code please, thank you

<select name="agent" id="agent">
</select>  

推荐答案

为此,您需要遍历查询结果的每一行,并将此信息用于每个下拉选项.您应该能够很容易地调整下面的代码以满足您的需要.

To do this you want to loop through each row of your query results and use this info for each of your drop down's options. You should be able to adjust the code below fairly easily to meet your needs.

// Assume $db is a PDO object
$query = $db->query("YOUR QUERY HERE"); // Run your query

echo '<select name="DROP DOWN NAME">'; // Open your drop down box

// Loop through the query results, outputing the options one by one
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
   echo '<option value="'.$row['something'].'">'.$row['something'].'</option>';
}

echo '</select>';// Close your drop down box

这篇关于从 MySQL 数据库中获取数据到 html 下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆