【问题标题】:Preselect dropdown options in a MySQL database table using php MySQLi [closed]使用 php MySQLi 在 MySQL 数据库表中预选下拉选项 [关闭]
【发布时间】:2022-01-10 11:01:19
【问题描述】:

我正在使用 MySQLi 编写一个 PC 修复应用程序。我的数据库中有一个名为 "item_for_repair" 的 ENUM 行。在 ENUM 列表中是笔记本电脑、台式机等。

在我的前端 html 表上,当我单击特定工作卡上的编辑时,在“待维修项目”下,有一个下拉菜单始终选择第一个值(例如笔记本电脑)而不是实际项目已存储(例如,桌面),我需要将下拉列表预选与存储作业卡时最初存储在数据库中的维修项目。

这是正在发生的事情的示例图像。数据库中要修复的项目是台式机,但在这里你可以清楚地看到它显示的笔记本电脑。

我的代码连接到数据库以及从数据库中选择所有代码:

<?php include 'settings.php'; //This file has all the database connection settings etc.

?>
 <?php
$eid=$_GET['editid'];
$ret=mysqli_query($conn,"select * from pcrepairs where job_number='$eid'");
while ($row=mysqli_fetch_array($ret)) {
?>

这里是选择框代码:

<select name="item_for_repair" id="item_for_repair">
    <option value="Laptop" <?= $item_for_repair === 'Laptop' ? 'selected' : '' ?>>Laptop</option>
    <option value="Desktop" <?= $item_for_repair === 'Desktop' ? 'selected' : '' ?>>Desktop</option>
    <option value="Television" <?= $item_for_repair === 'Television' ? 'selected' : '' ?>>Television</option>
    <option value="Washing Machine" <?= $item_for_repair === 'Washing Machine' ? 'selected' : '' ?>>Washing Machine</option>
    <option value="Tumble Dryer" <?= $item_for_repair === 'Tumble Dryer' ? 'selected' : '' ?>>Tumble Dryer</option>
    <option value="Dishwasher" <?= $item_for_repair === 'Dishwasher' ? 'selected' : '' ?>>Dishwasher</option>
    <option value="Microwave" <?= $item_for_repair === 'Microwave' ? 'selected' : '' ?>>Microwave</option>
    <option value="Fridge" <?= $item_for_repair === 'Fridge' ? 'selected' : '' ?>>Fridge</option>
    <option value="Printer" <?= $item_for_repair === 'Printer' ? 'selected' : '' ?>>Printer</option>
    <option value="Other" <?= $item_for_repair === 'Other' ? 'selected' : '' ?>>Other</option>
</select>

我哪里出错了?

【问题讨论】:

  • 只是自学。我将更新问题并添加我的 PDO 代码。
  • @Dharman,参见 EDIT 2。它有点乱,但它是一个仅供我个人学习的内部应用程序。我知道安全漏洞。
  • 不推荐使用常量FILTER_SANITIZE_STRING。请停止使用。
  • 什么安全漏洞?无论如何,我认为在转换为 mysqli 时,这部分导致您出现问题仍不清楚。请更具体。我看到一个巨大的 PDO 准备声明,但我不确定您对其中的哪一部分感到困惑。我们不需要看所有代码,只看与这个问题相关的部分即可。
  • header('Location: ...');之后总是exit()

标签: php mysql mysqli


【解决方案1】:

这是一个完整的示例,说明如何使用 MySQLi 从 MySQL 数据库中填充 HTML select 检索数据

<?php

class Country {
    public $code;
    public $name;
    
    function __construct($code, $name) {
        $this->code = $code;
        $this->name = $name;
    }
}

define("HOST", "localhost");
define("USERNAME", "root");
define("PASSWD", "");
define("DBNAME", "shop");

function country_select_list() {
    $array = null;
    $link = mysqli_connect(HOST, USERNAME, PASSWD, DBNAME);
    $stmt = mysqli_prepare($link, "select code, name from country order by name");
    if (!mysqli_execute($stmt)) {
        throw new Exception(mysqli_stmt_error($stmt));
    }
    $code = null;
    $name = null;
    mysqli_stmt_bind_result($stmt, $code, $name);
    while (mysqli_stmt_fetch($stmt)) {
        $array[] = new Country($code, $name);
    }
    mysqli_stmt_close($stmt);
    mysqli_close($link);
    return $array;
}

$user_country = 'CHE';

?>
<select id="user_country" name="user_country">
    <option value=""></option>
<?php

foreach (country_select_list() as $country) {
    echo '    <option value="' . $country->code . '"';
    if (isset($user_country) && $user_country == $country->code) {
        echo ' selected="selected"';
    }
    echo ">" . $country->name . "</option>\r\n";
}

?>
</select>

这是输出

<select id="user_country" name="user_country">
    <option value=""></option>
    <option value="ITA">Italy</option>
    <option value="CHE" selected="selected">Switzerland</option>
</select>

【讨论】:

  • 请解释您展示的内容
  • 我回复了答案,提供了一个示例,说明如何使用 MySQLi 从 MySQL 数据库中填充 HTML“选择”检索数据 - 我也会将此信息添加到帖子中
猜你喜欢
  • 2011-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-29
相关资源
最近更新 更多