How to Create a Dynamic Drop Down List in PHP populated from MySQL Database(如何在从 MySQL 数据库填充的 PHP 中创建动态下拉列表)
问题描述
我正在尝试使用 PHP 和 mysql 数据库创建一个动态下拉列表.我编写了以下代码并给了我输出,但问题是它为每个项目显示不同的下拉菜单,我希望所有项目都在一个下拉列表中.请检查并指导我.
I am trying to create a dynamic drop down list using PHP and mysql database. I have written the following code and its giving me the output, but the problem is that its showing different drop-down menu for each item, I want all items in a single drop down list. Kindly check it and guide me.
$select_query= "Select name from category";
$select_query_run = mysql_query($select_query);
while ($select_query_array= mysql_fetch_array($select_query_run) )
{
foreach ($select_query_array as $select_query_display)
{
echo "
<select>
<option value='' >$select_query_display</option>
</select>
";
}
}
谢谢
推荐答案
摆脱内部的 foreach 循环......它对你没有任何作用,并将开始和结束选择标签移到 while 循环之外.
Get rid of the inner foreach loop... it is doing nothing for you and move the start and end select tags outside of the while loop.
$select_query= "Select name from category";
$select_query_run = mysql_query($select_query);
echo "<select name='category'>";
while ($select_query_array= mysql_fetch_array($select_query_run) )
{
echo "<option value='' >".htmlspecialchars($select_query_array["name"])."</option>";
}
echo "</select>";
这篇关于如何在从 MySQL 数据库填充的 PHP 中创建动态下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在从 MySQL 数据库填充的 PHP 中创建动态下拉列表


- PHP - if 语句中的倒序 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01