0

I have a multiple step form where users can add supply items and parts. I'm trying to save this info to a MySQL database in one record but for some reason its only saving 1 item from the array. Any ideas?

 <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");
});
});

And the 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')");
user1235709
  • 43
  • 1
  • 7

1 Answers1

0

If I understand your question correctly, then you need to make your form input fields, array input fields. like this:

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

read this topic for more details: How to get form input array into PHP array

Community
  • 1
  • 1
hbourchi
  • 309
  • 2
  • 8