表格循环行中的php mysql更新数组

时间:2018-01-07 08:34:36

标签: php mysql

如何通过一次提交更新多行?当我更新一行时,另一行将更新为相同的值。如何使用一次提交单独更新每一行?

这是我的php:

<?php
while($receipt_row = mysqli_fetch_array($receipt_fetch)){
    echo"
<tr>
    <td>
    <input type='text'  name='item[]'value='$receipt_row[item]' required> 
    </td>
    <td class='text-right'>
    <input type='number'name='quantity[]' value='$receipt_row[quantity]' required>
    </td>
    <td class='text-right'>
    <input type='number'name='price[]' value='$receipt_row[price]'required>
    </td>
    <td class='text-right'>
    <input type='text' name='discount[]' value='$receipt_row[discount]'>
    </td>
</tr>";}
?>
 <input type="submit" value="Update Receipt" name="submit">

html表格:

{{1}}

1 个答案:

答案 0 :(得分:1)

我希望以下内容能够说明如何实现您的目标。将其复制为新的演示脚本并运行它以查看最终结果。注释掉的代码是您应该如何进行真正的更新,但如下所述,在上面的注释中,不清楚原始代码示例中$ rcpt的定义...

通过在每个表行中包含一个隐藏字段,您可以在POST数据数组中索引该特定ID,并使用该字段使用该特定表行(或该表行中的表单元素)中的值更新该特定记录

演示模拟已填写的表单(在本例中为随机数据) - 并且在提交时将显示将运行的有效结束查询。从那里你应该判断输出是否符合预期,然后为你的实际代码实现prepared statement方法。

<!doctype html>
<html>
    <head>
        <meta charset='utf-8' />
        <title>Multi field post update demo</title>
    </head>
    <body>
        <?php
            if( $_SERVER['REQUEST_METHOD']=='POST' ){
                if( isset( $_POST['submit'], $_POST['id'], $_POST['item'], $_POST['quantity'], $_POST['discount'], $_POST['price'] ) && is_array( $_POST['id'] ) ){
                    /*

                    In practice you will use a prepared statement like this ~
                    though it is unclear where $rcpt is defined


                    $sql='update `receipt_tbl` set `receipt_no`=?, `item`=?, `quantity`=?, `price`=?, `discount`=? where `receipt_no`=?';
                    $stmt=$con->prepare( $sql );

                    if( $stmt ){
                        $stmt->bind_param( 'isiiii', $rcpt, $item, $qty, $price, $discount, $id );

                        foreach( $_POST['id']  as $index => $arr ){
                            $id=$_POST['id'][ $index ];
                            $item=$_POST['item'][ $index ];
                            $qty=$_POST['quantity'][ $index ];
                            $price=$_POST['price'][ $index ];
                            $discount=$_POST['discount'][ $index ];

                            $stmt->execute();
                        }
                        $stmt->free_result();
                        $stmt->close();
                        $con->close();
                    }


                    */

                    /* for demonstration only */
                    $sql='update `receipt_tbl` set `receipt_no`=:rcpt, `item`=:item, `quantity`=:qty, `price`=:price, `discount`=:discount where `receipt_no`=:id;';
                    $rcpt=404;

                    echo '<h1>example sql will effectively be</h1>';
                    foreach( $_POST['id']  as $index => $arr ){
                        $id=$_POST['id'][ $index ];
                        $item=$_POST['item'][ $index ];
                        $qty=$_POST['quantity'][ $index ];
                        $price=$_POST['price'][ $index ];
                        $discount=$_POST['discount'][ $index ];
                        echo str_replace( array(':rcpt',':item',':qty',':price',':discount',':id'), array($rcpt,$item,$qty,$price,$discount,$id), $sql ) . '<br />';
                    }
                }
            } else {
        ?>

        <!-- demo form -->
        <form method='post'>
            <table>
            <?php
                for( $i=0; $i < 10; $i++ ){
                    echo"
                        <tr>
                            <td>
                                <input type='text' name='item[]'value='item_$i' required /> 
                            </td>
                            <td class='text-right'>
                                <input type='number' name='quantity[]' value='".rand(1,100)."' required />
                            </td>
                            <td class='text-right'>
                                <input type='number' name='price[]' value='".rand(1,100)."' required />
                            </td>
                            <td class='text-right'>
                                <input type='text' name='discount[]' value='".rand(5,15)."' />
                                <input type='hidden' name='id[]' value='$i' /><!-- This should be the ID for this database record ~ presumably "receipt_no" -->
                            </td>
                        </tr>";
                }
            ?>
            </table>
            <input type='submit' name='submit' value='Update All Records' />
        </form>
        <?php

            }//close if/else

        ?>
    </body>
</html>