我有使用php和jQuery创建的动态添加的多行表

时间:2019-02-13 02:59:26

标签: php jquery mysql

我有一个动态添加的多行表,该表使用php和jQuery.html&jquery创建,运行良好。但是,当我将数据插入数据库时​​,它仅适用于单行。当我想添加多行时,显示错误。

当我想插入多行时,它仅适用于第一行并显示错误....

  

致命错误:未捕获的PDOException:SQLSTATE [23000]:违反完整性约束:1048 E:\ xampp \ htdocs \ atiq \ New-Project \ ButtonTexMine \ name.php:47列'button_line'不能为空:堆栈跟踪: #0 E:\ xampp \ htdocs \ atiq \ New-Project \ ButtonTexMine \ name.php(47):PDOStatement-> execute(Array)#1 {main}抛出E:\ xampp \ htdocs \ atiq \ New-Project \ ButtonTexMine \ name.php在第47行

有人请帮助吗?我的插入查询如下:

    <form action="name.php" method="post" name="add_name" id="add_name">
                    <div class="row" id="div1"  style="background-color:#fafbf6;color:#000;">
                        <div class="col-xs-2">
                            <div class="form-group">
                                <label>Date</label>
                                <div>
                                    <div class="input-group">
                                        <input name="date" type="text" class="form-control" placeholder="mm/dd/yyyy" id="datepicker-autoclose">
                                        <span class="input-group-addon bg-custom b-0"><i class="mdi mdi-calendar text-white"></i></span>
                                    </div><!-- input-group -->
                                </div>
                            </div>
                        </div>
                        <div class="col-xs-2">
                            <div class="form-group">
                                <label>Party Name</label>
                                <input type="text" class="form-control" maxlength="100" name="party_name" id="placement" /> 
                            </div>
                        </div>
                        <div class="col-xs-2">
                            <div class="form-group">
                                <label>Party Address</label>
                                <input type="text" class="form-control" maxlength="500" name="party_address" id="placement" />  
                            </div>
                        </div>
                        <div class="col-xs-2">
                            <div class="form-group">
                                <label>Party Ref</label>
                                <input type="text" class="form-control" maxlength="100" name="party_ref" id="placement" />  
                            </div>
                        </div>
                        <div class="col-xs-2">
                            <div class="form-group">
                                <label>Work Order No</label>
                                <input type="text" class="form-control" maxlength="30" name="won" id="placement" /> 
                            </div>
                        </div>
                        <div class="col-xs-2">
                            <div class="form-group">
                                <label>Work Order Off</label>
                                <input type="text" class="form-control" maxlength="100" name="woof" id="placement" />   
                            </div>
                        </div>
                    </div>
                    <div class="row" id="div1"  style="background-color:#fafbf6;color:#000;">
                        <div class="table-responsive">
                            <table class="table table-bordered" id="dynamic_field">
                            <thead>
                                <th>Button Line</th>
                                <th>Button Hole</th>
                                <th>Button Type</th>
                                <th>Logo Button</th>
                                <th>Qty</th>
                                <th>Rate</th>
                                <th>Amount</th>
                                <th>Revised Rate</th>
                                <th>Revised Amount</th>
                                <th></th>
                            </thead>
                            <tbody>
                                <tr>
                                    <td><select name="button_line[]" class="form-control name_list">
                                                <option value=''>Select Option</option>
                                                <option value="14L">14L</option>
                                                <option value="16L">16L</option>
                                                <option value="18L">18L</option>
                                                <option value="20L">20L</option>
                                                <option value="22L">22L</option>
                                                <option value="24L">24L</option>
                                                <option value="26L">26L</option>
                                                <option value="28L">28L</option>
                                                <option value="30L">30L</option>
                                                <option value="32L">32L</option>
                                                <option value="36L">36L</option>
                                                <option value="40L">40L</option>
                                                <option value="44L">44L</option>
                                                <option value="48L">48L</option>
                                                <option value="54L">54L</option>
                                                <option value="60L">60L</option>
                                                </select> </td>
                                    <td><select name="button_hole[]" class="form-control name_list">
                                                <option value=''>Select Option</option>
                                                <option value='2'>2</option>
                                                <option value='3'>3</option>
                                                <option value='4'>4</option>
                                                <option value='others'>Otheres</option>
                                            </select> </td>
                                    <td><select name="button_type[]" class="form-control name_list">
                                                <option value=''>Select Option</option>
                                                <option value='Pearl'>Pearl</option>
                                                <option value='Chalk'>Chalk</option>
                                                <option value='Horn'>Horn</option>
                                                <option value='T.P.T'>T.P.T</option>
                                            </select> </td>
                                    <td><select name="logo_button[]" class="form-control name_list">
                                                <option value=''>Select Option</option>
                                                <option value='yes'>Yes</option>
                                                <option value='no'>No</option>
                                            </select> </td>
                                    <td><input type="text" name="qty[]" id="qty0" onchange="sum(0)" placeholder="Enter your Qty" class="form-control name_list" /></td>
                                    <td><input type="text" name="rate[]" id="rate0" onchange="sum(0)" placeholder="Enter your Rate" class="form-control name_list" /></td>
                                    <td><input type="text" name="total[]" id="sum0" class="form-control name_list" /></td>
                                    <td><input type="text" name="r_rate[]" id="Rrate0" onchange="sum(0)" placeholder="Enter your Rate" class="form-control name_list" /></td>
                                    <td><input type="text" name="r_amount[]" id="Rsum0" class="form-control name_list" /></td>
                                    <td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
                                </tr>
                            </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-xs-2">
                            <div class="form-group">
                                <label>Proforma Invoice Number</label>
                                <input type="text" class="form-control" maxlength="30" name="pro_in_no" id="placement" />   
                            </div>
                        </div>
                        <div class="col-xs-2">
                            <div class="form-group">
                                <label>Proforma Invoice Date</label>
                                <input type="text" class="form-control" maxlength="30" name="pro_in_date" id="placement" /> 
                            </div>
                        </div>
                        <div class="col-xs-2">
                            <div class="form-group">
                                <label>Cash Bill Number</label>
                                <input type="text" class="form-control" maxlength="100" name="cash_bill_no" id="" />    
                            </div>
                        </div>
                        <div class="col-xs-2">
                            <div class="form-group">
                                <label>LC SL No</label>
                                <input type="text" class="form-control" maxlength="30" name="lc_sl_no" id="" /> 
                            </div>
                        </div>
                        <div class="col-xs-2">
                            <div class="form-group">
                                <label>LC No</label>
                                <input type="text" class="form-control" maxlength="30" name="lc_no" id="" />    
                            </div>
                        </div>
                        <div class="col-xs-2">
                            <div class="form-group">
                                <label>LC Date</label>
                                <input type="text" class="form-control" maxlength="30" name="lc_date" id="placement" /> 
                            </div>
                        </div>
                        <div class="col-xs-2">
                            <div class="form-group">
                                <label>Total Amount</label>
                                <input type="text" class="form-control" maxlength="30" name="total_amount" id="allsum" />
                            </div>
                        </div>
                        <div class="col-xs-2">
                            <div class="form-group">
                                <label>Pay Amount</label>
                                <input type="text" class="form-control" maxlength="30" name="pay_amount" id="pay" />
                            </div>
                        </div>
                        <div class="col-xs-2">
                            <div class="form-group">
                                <label>Due</label>
                                <input type="text" class="form-control" maxlength="30" name="due" id="dueA" readonly />
                            </div>
                        </div>
                        <div class="col-xs-2">
                            <div class="form-group">
                                <label>Mode of Payment</label>
                                <select name="mode" class="form-control select2">
                                    <option>Select</option>
                                    <option value="Cash">Cash</option>
                                    <option value="LC">LC</option>
                                </select>    
                            </div>
                        </div>

                        <div class="col-xs-2">
                            <div class="form-group">
                                <div class="modal-footer">
                                    <input type="submit" name="submit" id="submit" class="btn btn-info" value="Submit" />
                                </div>    
                            </div>
                        </div>
                    </div>


                </form>
    <script>
var i=0;
$(document).ready(function(){
    $('#add').click(function(){
        i++;
        $('#dynamic_field').append('<tr id="row'+i+'"><td><select name="button_line['+i+']" class="form-control name_list"><option value="">Select Option</option><option value="14L">14L</option><option value="16L">16L</option><option value="18L">18L</option><option value="20L">20L</option><option value="22L">22L</option><option value="24L">24L</option><option value="26L">26L</option><option value="28L">28L</option><option value="30L">30L</option><option value="32L">32L</option><option value="36L">36L</option><option value="40L">40L</option><option value="44L">44L</option><option value="48L">48L</option><option value="54L">54L</option><option value="60L">60L</option></select></td><td><select name="button_hole['+i+']" class="form-control name_list"><option value="#">Select Option</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="others">Otheres</option></select> </td><td><select name="button_type['+i+']" class="form-control name_list"><option value="#">Select Option</option><option value="Pearl">Pearl</option><option value="Chalk">Chalk</option><option value="Horn">Horn</option><option value="T.P.T">T.P.T</option></select></td><td><select name="logo_button[]" class="form-control name_list"><option value="#">Select Option</option><option value="yes">Yes</option><option value="no">No</option></select></td><td><input type="text" name="qty['+i+']" id="qty'+i+'" onchange="sum(0)" placeholder="Enter your Qty" class="form-control name_list" /></td><td><input type="text" name="rate['+i+']" id="rate'+i+'" onchange="sum(0)" placeholder="Enter your Rate" class="form-control name_list" /></td><td><input type="text" name="total[]" id="sum'+i+'" class="form-control name_list" /></td><td><input type="text" name="r_rate[]" id="rate0" onchange="sum(0)" placeholder="Enter your Rate" class="form-control name_list" /></td><td><input type="text" name="r_amount[]" id="sum0" class="form-control name_list"/></td><td><button type="button" name="remove" id="'+i+'" class="btn btn-danger btn_remove">X</button></td></tr>');
        $('#qty'+i+', #rate'+i).change(function() {
            sum(i)
        });
    });

    $(document).on('click', '.btn_remove', function(){
        var button_id = $(this).attr("id"); 
        $('#row'+button_id+'').remove();
        sum_total();
    });

    $('#submit').click(function(){      
        $.ajax({
            url:"name.php",
            method:"POST",
            data:$('#add_name').serialize(),
            success:function(data)
            {
                alert(data);
                $('#add_name')[0].reset();
            }
        });
    });

});

$(document).ready(function() {
    //this calculates values automatically 
    sum(0);
});

function sum(i) {
    var qty1 = document.getElementById('qty'+i).value;
    var rate1 = document.getElementById('rate'+i).value;
    var result = parseFloat(qty1) * parseFloat(rate1);
    if (!isNaN(result)) {
        document.getElementById('sum'+i).value = result.toFixed(2);
    }
    sum_total();
}
function sum_total() {
    var newTot = 0;
    for (var a=0; a<=i ; a++) {
        aVal = $('#sum'+a);
        if (aVal && aVal.length) { newTot += aVal[0].value ? parseFloat(aVal[0].value) : 0; }
    }
    document.getElementById('allsum').value = newTot.toFixed(2);
}

</script>

<script>
        $(document).ready(function() {
            //this calculates values automatically 
            dueA();
            $("#allsum, #pay").on("keydown keyup", function() {
                dueA();
            });
        });

        function dueA() {
                    var allsum = document.getElementById('allsum').value;
                    var pay = document.getElementById('pay').value;
                    var result = parseFloat(allsum)- parseFloat(pay);
                    if (!isNaN(result)) {
                        document.getElementById('dueA').value = result.toFixed(2);
                    }
                }
        </script>

这里是name.php

    <?php
require_once("config.php");
if (!isset($_SESSION["user_id"]) || $_SESSION["user_id"] == "") {
    // not logged in send to login page
    redirect("index.php");
}


$query = "INSERT INTO orders (date, party_name, party_address, party_ref, work_order_no, work_order_off, button_line, button_hole, button_type, logo_button, quantity, rate, amount, revised_rate, revised_amount, pro_in_no, pro_in_date, cash_bill_no, lc_sl_no, lc_no, lc_date, total_amount, pay_amount, due, mode, status) VALUES (:a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :o, :p, :q, :r, :s, :t, :u, :v, :w, :x, :y, :z)";

for($count = 0; $count<count($_POST['qty']); $count++)
{
 $data = array(

':a' => $_POST['date'],
':b' => $_POST['party_name'],
':c' => $_POST['party_address'],
':d' => $_POST['party_ref'],
':e' => $_POST['won'],
':f' => $_POST['woof'],
':g' => $_POST['button_line'][$count],
':h' => $_POST['button_hole'][$count],
':i' => $_POST['button_type'][$count],
':j' => $_POST['logo_button'][$count],
':k' => $_POST['qty'][$count],
':l' => $_POST['rate'][$count],
':m' => $_POST['total'][$count],
':n' => $_POST['r_rate'][$count],
':o' => $_POST['r_amount'][$count],
':p' => $_POST['pro_in_no'],
':q' => $_POST['pro_in_date'],
':r' => $_POST['cash_bill_no'],
':s' => $_POST['lc_sl_no'],
':t' => $_POST['lc_no'],
':u' => $_POST['lc_date'],
':v' => $_POST['total_amount'],
':w' => $_POST['pay_amount'],
':x' => $_POST['due'],
':y' => $_POST['mode'],
':z' => 'pending'


 );
 $statement = $DB->prepare($query);
 $statement->execute($data);
}



header("location: orders.php");

?>

0 个答案:

没有答案