在表单中回显数组

时间:2012-02-01 11:48:19

标签: php

请我正在尝试更新表格,但它只更新了最后一行,我试图回复发送的帖子值(项目[Pidno]和项目[Pquantity])作为数组,但它回来时只是最后一行行项目[Pquantity]更新,并且它不返回项目[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
?>

更新脚本<​​/ p>

 $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();

2 个答案:

答案 0 :(得分:1)

根据我上面评论的更改,以下是我编写代码的方式:

<?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";
  }

答案 1 :(得分:0)

这在您的表单中始终相同,因此唯一的最后一个Pidno将在$ _POST [item]

name='item[Pidno]'

像以后一样做:

name='item[<?php echo $row['Pidno']; ?>]'