如何从php中的数据库表创建表单下拉列表?
16lz
2021-01-22
I am trying to code a function which creates a dropdown of school names selected from a database. It is doing fine creating a dropdown but it is not putting anything in the dropdown. Here is the code:
我正在尝试编写一个函数,该函数创建从数据库中选择的学校名称下拉列表。它正在创建一个下拉列表,但它没有在下拉列表中放置任何内容。这是代码:
function schoolDD($name, $selected){
$select = '';
if( $selected != null )
{
$select = $selected;
}
$qry = "select *
from school
order by name, id
where display = 'Y'";
$schools = _execQry($qry);
$html = '<select name="'.$name.'" >';
foreach( $schools as $s ){
$html .= '<option value="'. $s['id'] .'"';
if( $select == $s['name'] ){
$html .= 'selected="selected"';
}
$html .= '>'. $s['name'] . '</option>';
}
$html .= '</select>';
return $html;
}
3 个解决方案
#1
Problem solved. It was because in the query I had order before where. It should have been:
问题解决了。这是因为在我之前订购的查询中。应该是:
$qry = "select *
from school
where display = 'Y'
order by name, id";
Not:
$qry = "select *
from school
order by name, id
where display = 'Y'";
更多相关文章
- MySQL常用的函数
- MySQL查询返回用户的收件人列表
- Atitit 数据库 标准库  sdk 函数库 编程语言 mysql oracle
- C标准库中的函数定义在哪里?
- 如何使用Angularjs显示函数的返回值
- JavaScript 字符串函数 之查找字符方法(一)
- 如何将对象作为参数传播给函数?
- 来自jQuery的Javascript中的等效函数
- 如何在javascript函数中将URL编码为参数?