简体   繁体   English

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

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

In my database I have 2 tables: 在我的数据库中,我有2个表:

tabels To insert data, I have a form that populates dropdown options from the table formulation . 要插入数据,我有一个表格,该表格会填充表格formulation中的下拉选项。 This is what the insert form for formulation dropdown looks like: 这是用于配方下拉列表的插入形式如下所示:

<?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>

Now I am working on the 'Update' form. 现在,我正在处理“更新”表单。 But my question is how can I populate the 'Formulation' field dropdown with the data from the formulation table (like as the insert form) but pre-selected with the existing formulation value for the name from the items table? 但是我的问题是,如何用formulation表中的数据(例如插入表格)填充“配方”字段下拉列表,但要使用items表中name的现有formulation值进行预选? Like this image below: 如下图所示: UpdateForm

I am having problem with how I should build the form. 我在如何构建表单方面遇到问题。 How should I proceed with this form? 我应该如何处理此表格?

<?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>

Thanks in advance for your suggestion. 预先感谢您的建议。

Note: I'm not a user of mysqli so maybe there will be some error, but you will get the idea. 注意: 我不是mysqli的用户,所以也许会有一些错误,但是您会明白的。 This will not tackle the update part, just the populate part 这不会解决更新部分,而只解决填充部分

Since you are editing a certain item, I will assume that you have something to get the item's itemID . 由于您正在编辑某个项目,因此我假设您有一些东西可以获取该项目的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>

I changed the code to better suit the problem, there may be typos, just comment for clarification 我更改了代码以更好地解决该问题,可能有拼写错误,请添加注释以进行澄清

If I have understand Your question... You have to put Your result into a string. 如果我了解您的问题,则必须将结果放入字符串中。 For example: 例如:

<?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>

I hope to be of help 我希望能有所帮助

This should do the trick: 这应该可以解决问题:

<?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