PHP MySQL在更新之前只显示表中的一行


PHP MySQL only displaying one row in table prior to update

我有一个页面可以更新MySQL数据库中的现有配方记录。它引入了菜肴的细节,其中还包括一张单独的配料表。然而,我的代码只显示成分表中的一行,我认为这是ID最高的成分。我需要它来显示所有成分,但无法计算出代码中需要更改的内容。如果需要,它为用户提供了编辑和添加/删除(使用JavaScript)行的选项。

我是PHP的新手,所以我的代码可能效率不高,但它确实会更新记录。它只是不会在第一个实例中显示所有这些。

这是我的代码片段。Connection和$DishID在本页前面已经定义。

        <table id="Table" border="1">
        <tr>
            <td>#</td>
            <td>IngID</td>
            <td>Volume</td>
            <td>UnitID</td>
            <td>Delete</td>
            <td>Add</td>
        </tr>
        <tr>
            <td>1</td>
<?php
    $cdquery="SELECT i.IngID, i.IngName, di.Volume, i.UnitID 
        FROM Ing i
        join DishIng di
        on i.IngID = di.IngID
        Where di.DishID = $DishID";
            $cdresult=mysqli_query($con, $cdquery) or die ("Query to get data from ingredients failed: ".mysqli_error($con));
            while ($cdrow=mysqli_fetch_array($cdresult)) {
        $IngID=$cdrow["IngID"];
        $IngName=$cdrow["IngName"];
        $Volume=$cdrow["Volume"];
        $UnitID=$cdrow["UnitID"];
}
php?>
            <td><input type="text" name="IngID[]" value="<? echo $IngID; ?>"></td>
            <td><input type="text" name="IngName[]" value="<? echo $IngName; ?>"></td>
            <td><input type="text" name="Volume[]" value="<? echo $Volume; ?>"></td>       
            <td><input type="text" name="UnitID[]" value="<? echo $UnitID; ?>"></td>
            <td><input type="button" id="del" value="Delete" onclick="deleteRow(this)"/></td>
            <td><input type="button" id="add" value="Add" onclick="insRow()"/></td>
        </tr>
    </table>

非常感谢。

带上input insdie the while loop你的buttonwhile loop之外,就像这个

echo '<form>';
while ($cdrow=mysqli_fetch_array($cdresult)) {
        $IngID=$cdrow["IngID"];
        $IngName=$cdrow["IngName"];
        $Volume=$cdrow["Volume"];
        $UnitID=$cdrow["UnitID"];
         ?>
<td><input type="text" name="IngID[]" value="<? echo $IngID; ?>"></td>
            <td><input type="text" name="IngName[]" value="<? echo $IngName; ?>"></td>
            <td><input type="text" name="Volume[]" value="<? echo $Volume; ?>"></td>       
            <td><input type="text" name="UnitID[]" value="<? echo $UnitID; ?>"></td>
        </tr>
    </table>
<?php }?>
<td><input type="button" id="del" value="Delete" onclick="deleteRow(this)"/></td>
            <td><input type="button" id="add" value="Add" onclick="insRow()"/></td> 
            </form>

注意mysqli_不会自动保护您的应用程序,bind your value.

<table id="Table" border="1">
    <tr>
        <td>#</td>
        <td>IngID</td>
        <td>Volume</td>
        <td>UnitID</td>
        <td>Delete</td>
        <td>Add</td>
    </tr>

<php

$DishID = 'put the value here!!!!!!!';
$cdquery="
    SELECT 
        i.IngID,
        i.IngName,
        di.Volume,
        i.UnitID 
    FROM 
        Ing i,
        DishIng di
    WHERE 
        i.IngID = di.IngID
        di.DishID = '".$DishID."'
";
$cdresult = mysqli_query($con, $cdquery) or die ("Query to get data from ingredients failed: ".mysqli_error($con));
$i = 1;
while ($row = mysqli_fetch_all($cdresult, MYSQLI_ASSOC)) {

?>

    <tr>
        <td><?php echo $i; ?></td>
        <td><input type="text" name="IngID[]" value="<?php echo $row['IngID']; ?>"></td>
        <td><input type="text" name="IngName[]" value="<?php echo $row['IngName']; ?>"></td>
        <td><input type="text" name="Volume[]" value="<?php echo $row['Volume']; ?>"></td>       
        <td><input type="text" name="UnitID[]" value="<?php echo $row['UnitID']; ?>"></td>
        <td><input type="button" id="del" value="Delete" onclick="deleteRow(this)"/></td>
        <td><input type="button" id="add" value="Add" onclick="insRow()"/></td>
    </tr>

<php}?>