2014-10-31 62 views
0

我有一個頁面來更新MySQL數據庫中的現有配方記錄。它帶來了菜單的細節,其中還包括一個單獨的成分表。但是,我的代碼只在成分表中顯示一行,我認爲這是具有最高ID的成分。我需要它來顯示所有的成分,但不能計算出需要改變的代碼。如果需要,它可以讓用戶選擇編輯和添加/刪除(使用JavaScript)行。PHP MySQL僅在更新之前在表中顯示一行

我對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> 

非常感謝提前。

+0

一旦回波mysql_num_rows($ cdresult)?;並查看它返回的行數? – Choco 2014-10-31 10:01:12

回答

1

帶上你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.

+0

謝謝,工作過的魅力。 「綁定你的價值」是什麼意思? – fernalonso 2014-10-31 10:57:00

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

>

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