使用单个提交按钮将html数据保存在两个不同的mysql表中

时间:2015-05-29 04:45:38

标签: php html mysql

我有一个带有一些输入字段的页面和一个表,其行是使用jQuery动态生成的。我试图将输入字段保存在一个mysql表中,并将动态生成的行保存在另一个mysql表中。输入字段工作正常。但是对于动态生成的表行,只插入第一行,而第一行下面的其他roe不插入。这是我的代码。请告诉我我做错了什么。万分感谢。 HTML表格

<form class="form-horizontal" role="form" action="saveFiles/savePurchase.php" method="post" target="">
                <div class="form-group">
                    <label for="invoiceNo" class="col-md-6 control-label">Invoice No.</label>
                    <div class="col-md-6">
                        <input type="text" class="form-control" name="invoiceNo">
                    </div>
                </div>
                 <div class="form-group">
                    <label for="supplier" class="col-md-6 control-label">Supplier</label>
                    <div class="col-md-5">
                        <select name="supplier" class="form-control">
                            <option>SELECT</option>
                            <?php include('saveFiles/connection.php');
                                $query = mysql_query("SELECT Id,Name FROM supplier");
                                while($row = mysql_fetch_assoc($query)){
                                    $supplierId = $row['Id'];
                                    $supplierName = $row['Name'];?>

                                    <option value="<?php echo $supplierId;?>"><?php echo $supplierName;?></option>

                            <?php  }
                            ?>
                        </select>
                    </div>
                    <div class="col-md-1">
                        <img src="img/plus.png" class="pull-right" style="margin-top: 5px;" data-toggle="modal" data-target="#supplierModal">
                    </div>
                </div>

        </div>
        <div class="col-md-4">

                <div class="form-group">
                    <label for="invoiceNo" class="col-md-6 control-label">Invoice Date</label>
                    <div class="col-md-6">
                        <input type="text" class="form-control datepicker" name="invoiceDate" readonly="readonly">
                    </div>
                </div><br><br>
                 <div class="form-group">
                    <label for="paymentMode" class="col-md-6 control-label">Payment Mode</label>
                    <div class="col-md-6">
                        <select name="paymentMode" class="form-control">
                            <option>Cash</option>
                            <option>Cheque</option>
                            <option>Card</option>
                            <option>On Account</option>
                            <option>Credit</option>
                        </select>
                    </div>
                </div>

        </div>
        <div class="col-md-3"></div>
    </div>
    <div class="row">
        <div class="col-md-12">

                <table class="table table-condensed table-responsive">
                    <thead>
                        <th>Product Name</th>
                        <th>Unit</th>
                        <th>Qty</th>
                        <th><button type="button" class="btn btn-info" id="addNew">Add New Row</button></th>
                    </thead>
                    <tbody>
                        <tr class="cloneme">
                            <td>
                                <select name="productName[]" class="form-control" style="width: 110px;">
                                    <option>SELECT</option>
                                    <?php
                                        include('saveFiles/connection.php');
                                        $selectQuery = mysql_query("SELECT Id,ProductName FROM items ORDER BY ProductName");
                                        while($rows = mysql_fetch_assoc($selectQuery)){
                                            $productId = $rows['Id'];
                                            $productName = $rows['ProductName']; ?>

                                            <option value="<?php echo $productId;?>"><?php echo $productName?></option>
                                    <?php  }
                                    ?>

                                </select>
                            </td>

                            <td> <input type="text" class="form-control" name="unit[]"></td>
                            <td> <input type="text" class="form-control" name="qty[]"></td>


                        </tr>
                    </tbody>
                </table>
                <div class="row">
                    <div class="col-md-12 text-center">
                        <button class="btn btn-success" type="submit" name="submit" id="save">Save</button>
                        <button class="btn btn-danger" type="reset" name="reser">Clear</button>
                    </div>
                </div>
            </form>
            <div class="row">
                <div class="col-md-12 text-center">
                    <iframe name="purchaseMsg" style="border: none; background-color: #00aceb; !important; width:0px; height:0px;"></iframe>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="supplierModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button>
                <h4 class="modal-title">Add Supplier</h4>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <form action="saveSupplier" method="post" target="categoryMsg">
                        <div class="row">
                            <div class="col-md-4"></div>
                            <div class="col-md-4">
                                <input type="text" class="form-control" name="newSupplier" placeholder="Add New Supplier">
                            </div>
                            <div class="col-md-4"></div>
                        </div><br>
                        <div class="row">
                            <div class="col-md-4"></div>
                            <div class="col-md-4">
                                <input type="button" class="btn btn-success"value="Save" id="saveSupplier" data-dismiss="modal">
                                <input type="reset" class="btn btn-danger" value="Clear">
                            </div>
                            <div class="col-md-4"></div>
                        </div>
                    </form>

这是我的php脚本来插入表单。

<?php
include('connection.php');

if(isset($_POST['submit'])){
    $invoiceNo = $_POST['invoiceNo'];
    $invoiceDate = $_POST['invoiceDate'];
    $supplierId = $_POST['supplier'];

    $insertQuery = mysql_query("INSERT INTO purchase(InvoiceNo,InvoiceDate,SupplierId) VALUES('$invoiceNo','$invoiceDate','$supplierId')");

    $id = mysql_insert_id();
    $rowData = array();
    foreach($_POST['productName'] as $row=>$itemName){
        $productName = mysql_real_escape_string($itemName);
        $unit = mysql_real_escape_string($_POST['unit'][$row]);
        $qty = mysql_real_escape_string($_POST['qty'][$row]);



        $rowData[] = "('$productName','$unit','qty')";
    }
    $query=mysql_query("INSERT INTO purchaseDetails(ItemName, Unit, Quantity)  VALUES".implode(',',$rowData));
}

?>

请帮我解决这个问题。谢谢。

1 个答案:

答案 0 :(得分:-1)

我认为这可能与你构建多个插入记录的方式有关,所以不要这样......

rowData[] = "('$productName','$unit','qty')";

你应该尝试这样的事情......

rowData[] = "(\"".$productName."\", \"".$unit."\", \"".$qty."\")";