将html表多行插入数据库

时间:2015-03-21 06:24:22

标签: php jquery mysql

我试图将html表多行数据插入数据库,但没有错误就没有成功。

请参考下面的我的代码,并建议我错过了什么。

检索下拉选择的数据

<?php
    //including the database connection file
    include_once("config_db.php");
    $gettminfo=mysql_query("SELECT * FROM tb_tm ORDER BY tm_abb"); 
?>

表格的Html代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="css/spa_invoice.css"/>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>

<body>
    <!--Add & delete rows from table dynamically-->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script>
        $(function(){
            $('#addRow').click(function(){
                var html = $('#row_template').html();
                $('#dataTable').append(html);
                $(".tablerow").each(function(index) {
                    $(this).html(index + 1);
                });
            });
            $('#deleteRow').click(function(){
                $('#dataTable .mychkbox:checked').parents('tr').remove();
                calc_ttl();
            });
            $('#dataTable').on('change','.select-desc',function(){
                var cur_val = $(this).val();
                $(this).parents('tr').find('input[name="order_unit_prc[]"]').val(cur_val);
            });
            $('#dataTable').on('keyup','input[name="order_qty[]"]', function(){
                var qty = +$(this).val();
                var unit = +$(this).parents('tr').find('input[name="order_unit_prc[]"]').val();
                $(this).parents('tr').find('input[name="order_amt[]"]').val(qty*unit);

                calc_ttl();
            });
        });
    </script>

    <form action="" method="post" name="form_spa_inv">

        <fieldset class="row3">
            <table>
                <tr>
                    <td><input type="button" value="Add Treatment" id="addRow" /></td>
                    <td><input type="button" value="Remove Treatment" id="deleteRow" /></td>
                </tr>
            </table>
            <table id="dataTable" class="form" border="1">
                <tr>
                    <td></td>
                    <td>No</td>
                    <td>Description</td>
                    <td>Qty</td>
                    <td>Unit Price</td>
                    <td>Amount</td>
                </tr>
            </table>

            <!-- table row template here -->
            <table id="row_template" style="display:none">
                <tr>
                    <td><input type="checkbox" name="chk[]" class="mychkbox" /></td>
                    <td class="tablerow"></td>
                    <td>
                        <select name="order_desc[]" id="order_desc" class="select-desc">
                            <option value="">-- Select Treatment --</option>
                            <?php
                                    while($dd=mysql_fetch_array($gettminfo)) {
                        ?>
                                    <option value="<?php echo $dd['tm_unit_prc']?>"><?php echo $dd['tm_abb'] ?> - <?php echo $dd['tm_desc'] ?></option>
                        <?php
                                }
                            ?>
                            </select>
                    </td>
                    <td>
                        <input type="text" name="order_qty[]" id="order_qty" size="10">
                    </td>
                    <td>
                        <input type="text" name="order_unit_prc[]" id="order_unit_prc" readonly>
                    </td>
                    <td>
                        <input type="text" name="order_amt[]" id="order_amt" readonly>
                    </td>
                </tr>
            </table>
        </fieldset>

        <p style="text-align: center;"><input type="submit" name="Submit" value="Create">
    </form>
</body>
</html>

插入mysql查询

<?php
    //including the database connection file
    include_once("config_db.php");

    if(isset($_POST['Submit']))
    {       
        for($i=0; $i < count($_POST['order_desc']); $i++) {
            $order_desc = addslashes($_POST['order_desc'][$i]);
            $order_qty = addslashes($_POST['order_qty'][$i]);
            $order_unit_prc = addslashes($_POST['order_unit_prc'][$i]);
            $order_amt = addslashes($_POST['order_amt'][$i]);

            //insert data to database tb_odr_dtl
            $insert_odrdtl=mysql_query("INSERT INTO tb_odr_dtl(order_desc,order_qty,order_unit_prc,order_amt) VALUES('$order_desc','$order_qty','$order_unit_prc','$order_amt')");
            mysql_query($insert_odrdtl);

            //display success message
            if($insert_odrdtl) {
                echo "<script>alert('Invoice created successfully!')</script>";
                echo "<script>window.open('cv.php','_self')</script>";
            }
        }       
    }
?>

1 个答案:

答案 0 :(得分:0)

将服务器站点代码更改为以下内容:

<?php
//including the database connection file
include_once("config_db.php");

if(isset($_POST['Submit']))
{       
    for($i=0; $i < count($_POST['order_desc']); $i++) {
        $order_desc = addslashes($_POST['order_desc'][$i]);
        $order_qty = addslashes($_POST['order_qty'][$i]);
        $order_unit_prc = addslashes($_POST['order_unit_prc'][$i]);
        $order_amt = addslashes($_POST['order_amt'][$i]);

        //insert data to database tb_odr_dtl
        $insert_odrdtl=mysql_query("INSERT INTO tb_odr_dtl(order_desc,order_qty,order_unit_prc,order_amt) VALUES('$order_desc','$order_qty','$order_unit_prc','$order_amt')");
  

//的mysql_query($ insert_odrdtl); //删除这行代码

        //display success message
        if($insert_odrdtl) {
            echo "<script>alert('Invoice created successfully!')</script>";
            echo "<script>window.open('cv.php','_self')</script>";
        }
    }       
}

&GT;