Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何用另一个MySQL表中的现有数据填充预先选择的下拉字段?_Php_Jquery_Mysql_Forms - Fatal编程技术网

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

Php 如何用另一个MySQL表中的现有数据填充预先选择的下拉字段?,php,jquery,mysql,forms,Php,Jquery,Mysql,Forms,在我的数据库中,我有两个表: 要插入数据,我有一个表单,用于填充表formulation中的下拉选项。这是“公式插入表单”下拉列表的外观: <?php $formulation = ''; $query = "SELECT * FROM formulation"; $result = mysqli_query($connect, $query); while ($row = mysqli_fetch_array($result)) { $formulation .= '<o

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

要插入数据,我有一个表单,用于填充表
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>
提前感谢您的建议。

注意:我不是
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>

项目名称
保存更改

总体思路是更新特定项目?那么更新操作实际上更新了items表吗?是的,你是对的@谢谢你的建议。我正在研究它以适应我现有的系统。以后我一定会告诉你的。:)我不确定我使用的功能,所以在必要时更换它们。这怎么知道某个项目有什么样的公式?从我看到的情况来看,您正在
select
中嵌入项目的名称,而不是公式的名称。我的所有网页都使用此方法。元素名称可以替换为另一个元素。
<?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>