将数组保存到数据库

时间:2016-11-07 20:09:24

标签: php mysql

我有一个多步骤表单,用户可以在其中添加耗材和零件。我正在尝试将此信息保存到一个记录中的MySQL数据库中,但由于某种原因,它只能从数组中保存1个项目。有什么想法吗?

 <table id="suppliestable" class="table order-list3 table-hover table-condensed table-bordered" >
<thead>
<tr>
    <th width="30%">Supply Part #</th>
    <th width="30%">Supply Desc.</th>
    <th  size="4">Supply Price</th>
    <th>Supply Qty</th>
    <th>Total</th>
    <th></th>
</tr>
</thead>
<tbody>
<tr>
    <td><input type="text" class="input-small2" name="supplynumber"></td>
    <td><input type="text" class="input-small2" name="supplydescription" ></td>
    <td><input type="text" class="input-small2" name="supplyprice"   size="4" onblur="doCalc2(); calculate2();"></td>
    <td><input type="text" class="input-small2" name="supplyquantity"   size="4" onblur="doCalc2(); calculate2();"></td>
    <td>$<span class="amount2"></span> </td>
    <td><a class="deleteRow3"></a></td>
</tr>


</tbody>

 <tfoot>
    <tr>
        <td colspan="5" style="text-align: left;">
            <input type="button" id="addrow3" value="Add" class="btn btn-primary" />
        </td>
    </tr>

</tfoot>

$(document).ready(function () {
var counter = 0;

$("#addrow3").on("click", function () {
    doCalc2();
    calculate2();
    grandsum();

    counter = $('#suppliestable tr').length - 2;

    var newRow = $("<tr>");
    var cols = "";
    cols += '<td><input text="text" class="input-small2" name="supplynumber' + counter + '" /></td>';
    cols += '<td><input text="text" class="input-small2" name="supplydescription' + counter + '"/></td>';
    cols += '<td><input class="input-small2" size="4" type="text" name="supplyprice' + counter + '"  onblur="doCalc2(); calculate2();"/></td>';
    cols += '<td><input class="input-small2" size="4" type="text" name="supplyquantity' + counter + '"  onblur="doCalc2(); calculate2();"/></td>';
    cols += '<td>$<span class="amount2"></span></td>';
    cols += '<td><input type="button" class="ibtnDel btn btn-danger"  value="X"></td>';

    newRow.append(cols);
    if (counter == 100) $('#addrow3').attr('disabled', true).prop('value', "You've reached the limit");
    $("table.order-list3").append(newRow);
    counter++;
});

 $("table.order-list3").on("click", ".ibtnDel", function (event) {
    $(this).closest("tr").remove();
    doCalc2();
    calculate2();
    grandsum();

    counter -= 1
    $('#addrow3').attr('disabled', false).prop('value', "Add Row");
});
});

PHP:

    $supplynumber = $_POST['supplynumber'];
    $supplynumberarray = implode( ", ", $supplynumber);


    $supplydescription = $_POST['supplydescription'];
    $supplydescriptionarray = implode( ", ", $supplydescription);


    $supplyprice = $_POST['supplyprice'];
    $supplypricearray = implode( ", ", $supplyprice);


    $supplyquantity = $_POST['supplyquantity'];
    $supplyquantityarray = implode( ", ", $supplyquantity);


    $partnumber = $_POST['partnumber'];
    $partnumberarray = implode( ", ", $partnumber);


    $partdescription = $_POST['partdescription'];
    $partdescriptionarray = implode( ", ", $partdescription);


    $partprice = $_POST['partprice'];
    $partpricearray = implode( ", ", $partprice);


    $partquantity = $_POST['partquantity'];
    $partquantityarray = implode( ", ", $partquantity);

的MySQL

    $result = mysqli_query($mysqli, "INSERT INTO invoices( supplynumber, supplydescription, supplyprice, supplyquantity, partnumber, partdescription, partprice, partquantity, login_id) VALUES( '$supplynumberarray', '$supplydescriptionarray', '$supplypricearray', '$supplyquantityarray', '$partnumberarray', '$partdescriptionarray', '$partpricearray', '$partquantityarray', '$loginId')");

1 个答案:

答案 0 :(得分:0)

如果我正确理解了您的问题,那么您需要创建表单输入字段,数组输入字段。像这样:

<td><input type="text" class="input-small2" name="supplynumber[]"></td>

阅读此主题以获取更多详细信息: How to get form input array into PHP array