如何通过单击按钮添加更多行来更新表


How to update a table by adding more rows through the click of a button

我正在尝试用用户的请求在文本框中输入更新表。因此,如果用户希望在订单中输入5行数据,他们将在文本框中输入'5',点击更新按钮,它将用5行填充表格。我相信我的代码中有逻辑,但由于某种原因它没有更新。知道为什么吗?

<table>
<tr>
    <th class="textCol">Product Rows:</th>
    <td class="inputCol"><input type="text" name="rows"></td>
    <td><input class="update" type="button" name="update" value="Update"></td>
    <td class="inputCol"></td>
</tr>
</table>
<table class="bottomTable">
    <tr>
        <th class="textCol">Product</th>
        <th class="textCol">Quantity</th>
        <th class="textCol">Unit Price</th>
        <th class="textCol">Total Price</th>
    </tr>
<?
    if (isset($_POST['update']))
    {
        //Execute this code if the update button is clicked.
        $num = $_POST['rows'];
        for ($i=0; $i<$num; $i++) { ?>
          echo "<tr>
                    <td class="inputCol2"><input type="text" name="product[$i]" ></td>
                    <td class="inputCol2"><input type="text" name="quantity[$i]" ></td>
                    <td class="inputCol2">$<input type="text" name="unit[$i]" ></td>
                    <td class="inputCol2">$<input type="text" name="total[$i]" ></td>
                </tr>";
    <? } ?>
<? } else {?>
    <tr>
        <td class="textCol"></td>
        <td class="textCol"></td>
        <td class="textCol">Total Order:</td>
        <td class="inputCol2">$<input type="text" name="total6"></td>
    </tr>
<? } ?>
</table>

输入是否在表单中?如果它不在表单中,则默认情况下作为$_GET提交,而不是$_POST,因此If()将始终发现它为FALSE。

<form method="post">
<table>
    <tr>
        <th class="textCol">Product Rows:</th>
        <td class="inputCol"><input type="text" name="rows"></td>
        <td><input class="update" type="submit" name="update" value="Update"></td>
        <td class="inputCol"></td>
    </tr>
    <tr>
        <th class="textCol">Product</th>
        <th class="textCol">Quantity</th>
        <th class="textCol">Unit Price</th>
        <th class="textCol">Total Price</th>
    </tr>
<?
    if (isset($_POST['update']))
    {
        //Execute this code if the update button is clicked.
        $num = $_POST['rows'];
        for ($i=0; $i<$num; $i++) { ?>
            <tr>
                <td class="inputCol2"><input type="text" name="'product' . <?=[$i]?>" ></td>
                <td class="inputCol2"><input type="text" name="'quantity' . <?=[$i]?>" ></td>
                <td class="inputCol2">$<input type="text" name="'unit' . <?=[$i]?>" ></td>
                <td class="inputCol2">$<input type="text" name="'total' . <?=[$i]?>" ></td>
            </tr>
    <? } ?>
<? } else {?>
    <tr>
        <td class="textCol"></td>
        <td class="textCol"></td>
        <td class="textCol">Total Order:</td>
        <td class="inputCol2">$<input type="text" name="total6"></td>
    </tr>
<? } ?>
</table>
</form>

我的值的php仍然不正确,但我正在工作,目前。但是它现在正确地更新了行。谢谢你的帮助!