PHP for函数获取POSTED值


PHP for function get POSTED values

我有这个PHP/HTML代码:

<form method="post" action="create_quote2.php">
<table width="800" border="0" cellspacing="5" cellpadding="5">
  <tr>
    <td><strong>Select</strong></td>
    <td><strong>Image</strong></td>
    <td><strong>Name</strong></td>
    <td><strong>Sale Price</strong></td>
    <td><strong>Quantity</strong></td>
  </tr>
<?php
$sql="SELECT * from products ";
$rs=mysql_query($sql,$conn) or die(mysql_error());
$counter=0;
while($result=mysql_fetch_array($rs))
{
    $counter++;
    echo '<input type="hidden" name="product'.$counter.'" value="'.$_POST["checkbox$i"].'" />';
    echo '<tr>
                <td><input type="checkbox" value="'.$result["sequence"].'" name="checkbox'.$counter.'" /></td>
                <td>Image</td>
                <td>'.$result["title"].'</td>
                <td>&pound;'.$result["saleprice"].'</td>
                <td><input type="text" name="qty" id="qty" size="20" /></td>
              </tr>';
}
echo '<input type="hidden" name="counter" value="'.$counter.'" />';
?>
</table>
<input type="submit" name="submit" value="Next" />
</form>

所以当复选框被选中时,你会转到下一页,代码是:

<table width="800" border="0" cellspacing="5" cellpadding="5">
  <tr>
    <td><strong>Image</strong></td>
    <td><strong>Title</strong></td>
    <td><strong>Sale Price</strong></td>
    <td><strong>Trade Price</strong></td>
    <td><strong>Quantity</strong></td>
    <td><strong>Total Cost</strong></td>
  </tr>
<?php
for($i=1; $i<=$_POST["counter"]; $i++)
{
    if($_POST["checkbox$i"])
    {
        $counter++;
        $sql="SELECT * from products where sequence = '".$i."' ";
        $rs=mysql_query($sql,$conn) or die(mysql_error());
        $result=mysql_fetch_array($rs);     
        echo '<tr>
                    <td>Image</td>
                    <td>'.$result["title"].'</td>
                    <td>&pound;'.$result["saleprice"].'</td>
                    <td>&pound;'.$result["tradeprice"].'</td>
                    <td>&nbsp;</td>
                    <td>&nbsp;</td>
                  </tr>';   
    }
}
?>
</table>

它可以很好地工作,并从products表中选择所有正确的产品,但我需要一种方法来获得每行的过帐数量值。

如何在第二页显示已过帐的数量值?

附言:我不担心SQL在这段代码上的注入。。。

在的第一页使用<input type="text" name="qty'.$counter.'" id="qty'.$counter.'" size="20" />

然后相关小区中的$_POST["qty{$counter}"]$_POST['qty'.$i]

顺便说一句,你可能会发现使用HEREDOC结构更容易,这样你就不必一直在回声中添加引号:

echo <<<BLOCK
<tr>
    <td>Image</td>
    <td>{$result["title"]}</td>
    <td>&pound;{$result["saleprice"]}</td>
    <td>&pound;{$result["tradeprice"]}</td>
    <td>Quantity - {$_POST['qty'.$i]}</td>
    <td>&nbsp;</td>
</tr>
BLOCK;

我发现HEREDOC是一个很好的帮助,因为引号之间不会融合太多

只需更改数量即可:

<input type="text" name="qty" id="qty" size="20" />

<input type="text" name="qty'.$counter.'" id="qty" size="20" />

然后以与其他输入相同的方式引用。