2016-11-15 5 views
1

私は、ユーザが請求書フォームを編集できる編集ページを持っています。配列をテーブルにエコーするが、各配列をテーブルの新しい行に分ける必要がある。新しい行へのエコー配列

私が値をエコーすると、その値が1行にまとめられます。私は入力フィールドに値をエコーするとき、私は配列から1つのレコードを取得します。ここで

は、それが私のテーブルでは、次のようになります。その後、

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']; 


} 
> 

とテーブル: 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> 
:私はこれを取得

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

助けてください

EDIT *************

私はテーブルが動作したらセキュリティの問題を修正します。

私はこれを試みましたが、出力はまだ1行にあります。

<?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つのレコードに複数の値を持ち、それら私はカンマで区切るてるので、そののみ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 
} 
?> 
関連する問題