繁体   English   中英

如何用另一个MySQL表中的现有数据填充预选的下拉字段?

[英]How to populate dropdown field pre-selected with the existing data from another MySQL table?

在我的数据库中,我有2个表:

tabels 要插入数据,我有一个表格,该表格会填充表格formulation中的下拉选项。 这是用于配方下拉列表的插入形式如下所示:

<?php
$formulation = '';
$query = "SELECT * FROM formulation";
$result = mysqli_query($connect, $query);
while ($row = mysqli_fetch_array($result)) {
    $formulation .= '<option value="' . $row["formulationID"] . '">' . $row["formulation_name"] . '</option>';
}
?>

<select>
    <option value="">Select formulation</option>
    <?php echo $formulation; ?>
</select>

现在,我正在处理“更新”表单。 但是我的问题是,如何用formulation表中的数据(例如插入表格)填充“配方”字段下拉列表,但要使用items表中name的现有formulation值进行预选? 如下图所示: UpdateForm

我在如何构建表单方面遇到问题。 我应该如何处理此表格?

<?php
$output = array('data' => array());
$sql = "SELECT * FROM items";
$query = $connect->query($sql);

while ($row = $query->fetch_assoc()) {
    $output['data'][] = array(
        $row['name'],
    );
}
echo json_encode($output);
?>

<form action=" " method="POST">
    <div>
        <label>Name</label>
        <input type="text"><br>
        <label>Formulation</label>
        <select >
            <!--What should be the codes here? -->
        </select>
    </div>
    <button type = "submit">Save changes</button>
</form>

预先感谢您的建议。

注意: 我不是mysqli的用户,所以也许会有一些错误,但是您会明白的。 这不会解决更新部分,而只解决填充部分

由于您正在编辑某个项目,因此我假设您有一些东西可以获取该项目的itemID

<?php
$sql = "SELECT * FROM items WHERE itemID = ?";
$query = $connect->prepare($sql);
$query->bind_param("s", $yourItemID);
$query->execute();
$result = $query->fetch_assoc();
$itemName = $result['name'];
$itemFormulation = $result['formulation_fk'];

//now you have the name and the formulation of that certain item
?>

<form action=" " method="POST">
    <div>
        <label>Name</label>
        <input type="text" value="<?php echo $itemName; ?>"><br>
        <label>Formulation</label>
        <select >
            <?php
                $query = "SELECT * FROM formulation";
                $result = mysqli_query($connect, $query);
                while ($row = mysqli_fetch_array($result)) {
            ?>
                <option value="<?php echo $row['formulationID']; ?>" <?php echo ($row['formulationID'] == $itemFormulation) ? 'selected' : ''; ?>>
                    <?php echo $row['formulation_name']; ?>
                </option>
            <?php
                }
            ?>
        </select>
    </div>
    <button type = "submit">Save changes</button>
</form>

我更改了代码以更好地解决该问题,可能有拼写错误,请添加注释以进行澄清

如果我了解您的问题,则必须将结果放入字符串中。 例如:

<?php
$output = array('data' => array());
$sql = "SELECT * FROM items";
$query = $connect->query($sql);

$option = '';
while ($row = $query->fetch_assoc()) {
     $name=$row['name'],
    $option.='<option value="$name">$name</option>'
}
echo json_encode($output);
?>

<form action=" " method="POST">
    <div>
        <label>Name</label>
        <input type="text"><br>
        <label>Formulation</label>
        <select >
            <?=$option?>
        </select>
    </div>
    <button type = "submit">Save changes</button>
</form>

我希望能有所帮助

这应该可以解决问题:

<?php
$itemsSql = "SELECT * FROM items WHERE itemId = 5";
$itemQuery = $connect->query($sql);
$item = $itemQuery->fetch_assoc();

$formulationsSql = "SELECT * FROM formulation";
$formulationsQuery = $connect->query($sql);
$formulations = $itemQuery->fetch_assoc();
?>

<form action="updateItem" method="POST">
    <div>
        <label>Item Name</label>
        <input type="text" value="<?= $item[0]['name']; ?>"><br>
        <label>Formulation</label>
        <select>
            <?php foreach($formulations as $formulation){
            echo '<option value="'. $formulation['formulationId'].'">' .
                   $formulation['formulation_name'] . '</option>';
            } ?>
        </select>
    </div>
    <button type = "submit">Save changes</button>
</form>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM