2016-11-15 40 views
1

我有一個編輯頁面,用戶可以在其中編輯發票表單。我需要將數組回顯到表中,但將每個數組分隔成一個新的表格。Echo陣列換成新行

當我回應價值時,它聚集成一條線。當我將該值反映到輸入字段中時,我只從數組中獲取一條記錄。

下面是它看起來像在我的表:

enter image description here

<?php 

$id = $_GET['id']; 

$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE id=$id"); 

while($res = mysqli_fetch_array($result)) 
{ 

$partnumber = $res['partnumber']; 

$partdescription = $res['partdescription']; 

$partprice = $res['partprice']; 

$partquantity = $res['partquantity']; 


} 
> 

然後表:

<tr> 

    <td><input type="text" class="input-small" name="partnumber[]" value=<?php echo $partnumber;?>></td> 
    <td><input type="text" class="input-small" name="partdescription[]" value=<?php echo $partdescription;?>></td> 
    <td><input type="text" class="input-small" name="partprice[]" size="4" value=<?php echo $partprice;?>></td> 
    <td><input type="text" class="input-small" name="partquantity[]" size="4" value=<?php echo $partquantity;?>></td> 


</tr> 

我得到這個: enter image description here

<tr> 

    <td><?php echo $partnumber;?></td> 
    <td><?php echo $partdescription;?></td> 
    <td><?php echo $partprice;?></td> 
    <td><?php echo $partquantity;?></td> 


</tr> 

我得到這個: enter image description here

我嘗試以下,但他們都導致一個空白的回聲:

<tr> 
<td><? echo $res['partnumber']; ?></td> 
</tr><tr> 
<td><? echo $res['partdescription']; ?></td> 
</tr><tr> 
<td><? echo $res['partprice']; ?></td> 
</tr><tr> 
<td><? echo $res['partquantity']; ?></td> 
</tr> 

而且

<? foreach ($res as $row) { ?> 
<tr> 
<td><? echo $row['partnumber']; ?></td> 
<td><? echo $row['partdescription']; ?></td> 
<td><? echo $row['partprice']; ?></td> 
<td><? echo $row['partquantity']; ?></td> 
</tr> 
<? 
} 
?> 

這是我們的目標: enter image description here

請幫忙

編輯***************

我將解決安全問題,一旦我有表工作。

我試過了,輸出仍然在一條線上。

<?php 

$id = $_GET['id']; 


$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE id='".mysql_real_escape_string($id)."' "); 


while($res = mysqli_fetch_array($result)) 
{ 
?> 
<tr> 
    <td><input type="text" class="input-small" name="partnumber[]" value=<?php echo $res['partnumber'];?>></td> 
    <td><input type="text" class="input-small" name="partdescription[]" value=<?php echo $res['partdescription'];?>></td> 
    <td><input type="text" class="input-small" name="partprice[]" size="4" value=<?php echo $res['partprice'];?>></td> 
    <td><input type="text" class="input-small" name="partquantity[]" size="4" value=<?php echo $res['partquantity'];?>></td> 

<?php 
} 
?> 

這是否幫助?

enter image description here

enter image description here

請注意,它唯一的顯示1分的記錄,因爲我在一個記錄有多個值和他們,我用逗號隔開。請檢查上面的截圖。這裏的代碼:

$partnumber = $_POST['partnumber']; 
$partnumberarray = implode(", ", $partnumber); 


$partdescription = $_POST['partdescription']; 
$partdescriptionarray = implode(", ", $partdescription); 


$partprice = $_POST['partprice']; 
$partpricearray = implode(", ", $partprice); 


$partquantity = $_POST['partquantity']; 
$partquantityarray = implode(", ", $partquantity); 



$result = mysqli_query($mysqli, "INSERT INTO invoicespartnumber, partdescription, partprice, partquantity, login_id) VALUES('$partnumberarray', '$partdescriptionarray', '$partpricearray', '$partquantityarray', '$loginId')"); 

是否有無論如何我可以回顯1記錄中的值由逗號分隔成多行在表上?

+0

在MySQL語句中指定'ID'。你確定有多條記錄嗎? – Natsathorn

+0

*安全警告*:您的PHP是可注射的。使用這樣的SQL查詢時要非常小心。考慮一下page.php?id = 0或者drop table .. –

+0

@ user1235709請你給我們更多關於你的mysql表的更多細節。特別是,我真的很想看到你的'ID' – Natsathorn

回答

0

你必須把你的HTML裏面的while循環,你做結果數組。

它看起來像這樣。

<?php 

$id = $_GET['id']; 

$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE ID='".mysql_real_escape_string($id)."'"); 

while($res = mysqli_fetch_array($result)){ //close php here to easier write html. ?> 
<tr> 
     <td><input type="text" class="input-small" name="partnumber[]" value="<?php echo $res['partnumber'];?>"></td> 
     <td><input type="text" class="input-small" name="partdescription[]" value="<?php echo $res['partdescription'];?>"></td> 
     <td><input type="text" class="input-small" name="partprice[]" size="4" value="<?php echo $res['partprice'];?>"></td> 
     <td><input type="text" class="input-small" name="partquantity[]" size="4" value="<?php echo $res['partquantity'];?>"></td> 
</tr> 

<?php //open php tag again to add end scope to while loop 
} 
?> 
+0

修復注入安全問題是明智的 - 有人可能會因此造成很多**的損害。具體來說: WHERE ID ='「。mysql_real_escape_string($ id)。」'「 ..但強烈建議使用安全的mysqli函數。 –

+0

我會盡快解決安全問題 – user1235709

+0

@LukeBriggs當然我會更新它 – Natsathorn

0
<table width = "100%"> 
<thead> 
<tr> 
// here will be all columns names in th 
</tr> 
</thead> 
<tbody> 
<?php 

$id = $_GET['id']; 

$result = mysqli_query($mysqli, "SELECT * FROM invoices WHERE id=$id"); 

while($res = mysqli_fetch_array($result)) 
{ ?> 
<tr> 
<td><?php echo $res['partnumber']; ?></td> <!-- Also use input box here --> 

<td><?php echo $res['partdescription'];?><td> 

<td><?php echo $res['partprice']; ?></td> 
<td><?php echo $res['partquantity'];?></td> 
</tr> 
<?php 
} 
?>