Generate Drop Down From MySQL(从 MySQL 生成下拉菜单)
问题描述
我需要从 MySQL 表中的数据生成一个下拉菜单.
I need to generate a drop down menu from data in a MySQL table.
从表中,它必须获取用户 ID 和用户名.
From the table, it'll have to take the user id and the username.
然后它将用户 ID 设置为选项值,并将用户名设置为下拉列表中显示的内容.
Then it'll set the user id to option value and the username to what shows up in the drop down.
谁能给我看一些代码?我在进行以下工作时遇到问题:
Could anyone show me some code for it? I'm having trouble making the following work:
$sql = "SELECT user_id, user_name FROM users";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo "<option value=".$row['user_id'].">.$row['user_name'].</option>
";
echo "<option value="12275">".$row['user_name']."</option>
";
}
推荐答案
您的输出有几个问题.首先,从您的示例中不清楚您是否有实际的 select 元素父元素,而且我不确定浏览器是否会在没有父元素选择的情况下显示 options .其次,您没有转义数组变量.所以这可能会解决它:
You have a couple of issues with your output. First, it's not clear from your example if you have the actual select element parent, and I'm not sure if browsers will display options without a parent select. Second, you are not escaping your array variables. So this might fix it:
$sql = "SELECT user_id, user_name FROM users"; $result=mysql_query($sql);
echo '<select name="users">';
while($row = mysql_fetch_array($result)) {
echo '<option value="'. $row['user_id'] . '">' . $row['user_name'] . "</option>
";
}
echo '</select>';
这篇关于从 MySQL 生成下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从 MySQL 生成下拉菜单
基础教程推荐
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 如何替换eregi() 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
