Php 在PDO中使用数组值动态插入值

Php 在PDO中使用数组值动态插入值,php,mysql,arrays,pdo,Php,Mysql,Arrays,Pdo,我在将数组值插入数据库表时遇到问题。这是我创建发票的项目,使用动态添加(使用js)的新行将数组值添加到数据库表中。 我的php(PDO)代码如下: 我的表单值如下所示: 我的完整代码是: <form method="post"> <?php $party = $_GET['party']; $sq = $conn->query("SELECT * FROM invoice WHERE invoice_id = '$party'"); $ro = $sq->fet

我在将数组值插入数据库表时遇到问题。这是我创建发票的项目,使用动态添加(使用js)的新行将数组值添加到数据库表中。 我的php(PDO)代码如下: 我的表单值如下所示: 我的完整代码是:

<form method="post">
<?php $party = $_GET['party'];  
$sq = $conn->query("SELECT * FROM invoice WHERE invoice_id = '$party'");
$ro = $sq->fetch(PDO::FETCH_OBJ);
echo "Invoice for ".$ro->party;
?>
 <table class="table table-bordered table-hover" width="800">
<thead>
<tr>
<th> No </th>
<th> Particulars</th>
<th> Quantity</th>
<th> Price</th>
<th> Discount</th>
 <th> Amount</th>
<th> <input type="button" value="+" id="add" class="btn btn-primary" /> </th>
</tr>
</thead>
<tbody class="detail">
<input type="hidden" name="invoice_id[]" value="<?php echo $ro->invoice_id; ?>" />
<tr>
<td class="no">1 </td>


<input type="text" class="form-control productname" name="product_name[]" /> 
<td><input type="text" class="form-control quantity" name="quantity[]" /> </td>
<td><input type="text" class="form-control price" name="price[]" /> </td>
<td><input type="text" class="form-control discount" name="discount[]" /> </td>
<td><input type="text" class="form-control amount" name="amount[]" /> </td>
<td> <a href="#" class="remove">Delete </a></td>
</tr>
</tbody>
 <input type="submit" class="btn btn-primary" name="save" value="Save invoice" />



</form>

像这样命名你的字段

<input type="text" class="form-control productname" name="data[item1][product_name]" />
<input type="text" class="form-control productname" name="data[item1][amount]" />
...
<input type="text" class="form-control productname" name="data[item2][product_name]" />
<input type="text" class="form-control productname" name="data[item2][amount]" />

你读过说明书吗?当然应该有关于
stmt->execute()我已经这样做了,正如您所建议的:但是,仍然没有在db表中插入值:作为db表行视图作为:编辑删除4 6 0我认为我的问题在于字段名:实际上,[item1]字段是动态插入的,而不是手动插入的。如果您使用的是
数据[item1][product\u name]
名称然后您必须使用我的答案中的代码,
foreach($\u POST['data']as$row){
。我可以向您发送我的整个脚本,包括用于在创建发票时添加新行的java吗?然后,您可以理解我的建议。我非常厌倦不按原样工作代码。因为我正在使用:
83  7   Array   Array   Array   0   Array
<input type="text" class="form-control productname" name="data[item1][product_name]" />
<input type="text" class="form-control productname" name="data[item1][amount]" />
...
<input type="text" class="form-control productname" name="data[item2][product_name]" />
<input type="text" class="form-control productname" name="data[item2][amount]" />
if (isset($_POST['save'])) {
    $stmt = $conn->prepare("insert into invoicesubcategory
                (invoice_id,product_name,quantity,price,discount,amount)
                values (:invoice_id,:product_name,:quantity,:price,:discount,:amount)");

    foreach($_POST['data'] as $row){
        $stmt->execute($row);
    }
}