从MySQL生成下拉列表 [英] Generate Drop Down From MySQL

查看:481
本文介绍了从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>\n ";
  echo "<option value=\"12275\">".$row['user_name']."</option>\n ";
}

推荐答案

您的输出有两个问题.首先,不清楚您的示例是否具有实际的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>\n";
}

echo '</select>';

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

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