簡體   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