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


insert values using array value dynamically in PDO

>我在将数组值插入数据库表时遇到问题。这是我使用动态添加(使用 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>

我编写 JAVASCRIPT 来添加和删除上表的行以添加/删除值。

<?php 
 if (isset($_POST['save'])) {
$invoice_id = $_GET['party'];
$product_name = $_POST['product_name'];
$quantity = $_POST['quantity'];
$price = $_POST['price'];
$discount = $_POST['discount'];
$amount = $_POST['amount'];
for($i;$i<count($_POST['product_name']);$i++){
 $stmt = $conn->query("insert into invoicesubcategory (invoice_id,product_name,quantity,price,discount,amount)
values ('$invoice_id','$product_name','$quantity','$price','$discount','$amount')");
$stmt->bindParam( ':invoice_id', $invoice_id , PDO::PARAM_STR );
$stmt->bindParam( ':product_name', $product_name, PDO::PARAM_STR ); 
$stmt->bindParam( ':quantity', $quantity , PDO::PARAM_STR );
$stmt->bindParam( ':price', $price , PDO::PARAM_STR );
$stmt->bindParam( ':discount', $discount, PDO::PARAM_STR );
$stmt->bindParam( ':amount', $amount , PDO::PARAM_STR );
 header("location:invoicesubcat.php");  
}} ?>

我需要帮助,如何使用 PDO 在数组中插入值?问题是,我的代码,将值插入数据库表,如下所示:

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]" />

然后遍历$_POST['data']数组。

此外,您必须正确使用PHP。您必须使用预准备语句,以便正确设置查询格式和执行多个查询。

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