PHP MySQL Drop Down Box Populate Selected Value(PHP MySQL 下拉框填充选定值)
问题描述
我已经阅读了有关如何使用 MySQL 填充整个下拉列表的教程,但我遇到的问题是我只想从数据库中获取一个并将其作为选定的一个.所以我想要一个下拉列表,其中包含数据库中的三个项目(Item1、Item2、Item3),它存储在一个名为 itemschoice 的列中,该列的值为Item2".当我加载下拉框时,如何让 item2 被选中?
I have read tutorials about how to populate an entire Drop down list with MySQL, but the problem I am running into is that I only want to grab the one from the database and have that be the selected one. So I would have like a drop down with three items (Item1, Item2, Item3) in the database its stored in a column called itemschoice which has a value of 'Item2'. How do I go about getting item2 to be selected when I load the drop down box?
推荐答案
在您的 元素中为
selected
属性添加 中的值>itemschoice
.
In your <option>
element add the selected
attribute for the value that is in itemschoice
.
使用组合函数获取选择的粗略示例:
Crude example using a made up function to get the choice:
$choice = get_items_choice();
$results = mysqli_query($sql);
echo '<select name="whatever">';
while($row = mysqli_fetch_array($results)) {
if ($row['choice'] === $choice) {
echo '<option value="' . $choice . '" selected="selected" />';
} else {
echo '<option value="' . $choice . '" />';
}
}
echo '</select>';
这只是一个例子,不要复制&粘贴此内容而不添加某种错误验证!
This is just an example, don't copy & paste this without adding some kind of error verification!
这篇关于PHP MySQL 下拉框填充选定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PHP MySQL 下拉框填充选定值


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