繁体   English   中英

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 ,你的button之外while 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}?>

暂无
暂无

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

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