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

时间:2016-02-14 13:53:48

标签: 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->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在数组中插入值? 问题是,我的代码,将值插入到db表中,如下所示:

83  7   Array   Array   Array   0   Array

1 个答案:

答案 0 :(得分:0)

将您的字段命名为

<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。您必须为proper query formatting和多个查询执行使用预准备语句。

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