2016-11-07 149 views
0

我有一個多步表單,用戶可以在其中添加供應項目和零件。我試圖將這些信息保存到一個記錄中的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')"); 
+0

嘗試使用陣列'名稱= 「supplynumber []」'或'名稱= 「supplynumber [ '+計數器+']」' – AbraCadaver

+0

工作,謝謝 – user1235709

+1

你的表是否在每一列中都有逗號分隔的列表?這是一個可怕的設計。每個項目應該在不同的行中。 – Barmar

回答

0

如果我正確理解你的問題,那麼你需要讓你的表單輸入字段,數組輸入字段。像這樣:

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

閱讀本主題的更多細節: How to get form input array into PHP array