请注意,我正在尝试更新表单,但它只更新最后一行,我试图将发送的post值(item[Pidno]和item[Pquantity])作为数组进行回显,但它返回时只更新了最后一行item[Pqutity],并且没有返回item[Pidno],哪里出错了
<?
$pplresult = mysql_query("SELECT * FROM repplac");
echo "<table border='1'><tr><th> SHOP NAME</th><th> PRODUCT NAME</th><th>
PRODUCT SIZE</th><th> PRODUCT COLOUR</th><th> PRODUCT QUANTITY</th><th>
PRICE</th><th> </th></tr>";
while($row = mysql_fetch_assoc($pplresult)){?>
<form action='updatepplac.php' method='Post' class='slistbar'>
<?echo "<tr><td>" .$row['Sname'] ."</td><td>" .$row['Pname'] ."</td><td>" .$row['Psize'] ."</td><td>" .$row['Pcolour'] ."</td><td>" ."<input type='text' name='item[Pquantity]' id='Pquantity' value='{$row['Pquantity']}' >" ."</td><td>" .$row['Price'] ."</td><td>" ?>
<a href="deleteproduct.php?del=<?php echo $row['Pidno'];?>">delete</a></td></tr>
<input type='hidden' name='item[Pidno]' id='Pidno' value='<?php echo $row['Pidno']; ?>' ><?php }
// table closing tag
?>
更新脚本
$submit = $_POST['submit'];
//$rowdelete = $_GET['del'];
$nPquantity = $_POST['item'];
foreach($nPquantity as $Pquantitys){
list($pidno, $Pquantity) = $Pquantitys;
echo "$Pquantitys";
//echo "$rowdelete";
//echo "$pidno";
//echo "$Pquantity";
die();
根据我上面的评论进行的更改,以下是我将如何编写您的代码:
<?php
// Output start of table and form
echo "
<form action='updatepplac.php' method='Post' class='slistbar'>
<table border='1'>
<tr>
<th>SHOP NAME</th>
<th>PRODUCT NAME</th>
<th>PRODUCT SIZE</th>
<th>PRODUCT COLOUR</th>
<th>PRODUCT QUANTITY</th>
<th>PRICE</th>
<th></th>
</tr>";
// Get DB results and loop, outputting table rows with counter
$pplresult = mysql_query("SELECT * FROM repplac") or die(mysql_error());
for ($i = 0; $row = mysql_fetch_assoc($pplresult); $i++) {
echo "
<tr>
<td>".htmlspecialchars($row['Sname'])."</td>
<td>".htmlspecialchars($row['Pname'])."</td>
<td>".htmlspecialchars($row['Psize'])."</td>
<td>".htmlspecialchars($row['Pcolour'])."</td>
<td>
<input type='text' name='item[$i][Pquantity]' id='Pquantity' value='".htmlspecialchars($row['Pquantity'])."' />
<input type='hidden' name='item[$i][Pidno]' id='Pidno' value='".htmlspecialchars($row['Pidno'])."' />
</td>
<td>".htmlspecialchars($row['Price'])."</td>
<td><a href='deleteproduct.php?del=".htmlspecialchars($row['Pidno'])."'>delete</a></td>
</tr>";
}
// Close table and form
echo "
</table>
<input type='submit' value='Submit' />
</form>";
?>
然后在表单提交后,您可以循环提交的数据,如下所示:
<?php
$items = $_POST['item'];
foreach ($items as $key => $item) {
echo "Item $key: Pquantity={$item['Pquantity']}; Pidno={$item['Pidno']}<br>'n";
}
这在你的表单中总是一样的,所以只有最后一个Pidno会在$_POST[item]中
name='item[Pidno]'
以后怎么做:
name='item[<?php echo $row['Pidno']; ?>]'