PHP表单下拉列表包含基于SQL数据的选项 [英] PHP form dropdown containing options based on SQL data

查看:131
本文介绍了PHP表单下拉列表包含基于SQL数据的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有下拉字段的表单,我想使用波段数据库中存在的波段列表进行填充。我已经尝试了几段代码,但是下拉列表始终为空。我知道数据库连接很好,因为我在应用程序的多个部分中调用了连接。这是我对代码的尝试:

I have a form with a dropdown field which I would like to populate with a list of bands that exist in my 'bands' database. I have tried several pieces of code, but the dropdown list is always empty. I know that the DB connection is fine because I am calling the connection in several parts of my application. Here is my attempt at the code:

<?php

$select_query= "Select bandname from bands";
$select_query_run = mysql_query($select_query);
echo "<select name='bands'>";
while ($select_query_array=   mysql_fetch_array($select_query_run) )
{
        echo "<option value='' >".htmlspecialchars($select_query_array["bandname"])."</option>";
}
echo "</select>";

?>


推荐答案

添加 mysql_select_db 将解决您的问题

<?php
   // Report all PHP errors (see changelog)
   error_reporting(E_ALL);
   mysql_select_db("dbname") or die("Could not open the db");
   $select_query= "select bandname from bands";
   $select_query_run = mysql_query($select_query) or die(mysql_error();
   echo "<select name='bands'>";
    while ($select_query_array=   mysql_fetch_array($select_query_run) )
    {
        echo "<option value='' >".htmlspecialchars($select_query_array['bandname'])."</option>";
    }
    echo "</select>";
?>

这篇关于PHP表单下拉列表包含基于SQL数据的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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