[英]insert a multi-dimensional array to mysql with PHP
我試圖從動態jQuery表單中插入此多維數組,但不確定如何解決此問題。 打印時,數組顯示如下:
[item] => Array (
[1] => Array (
[unit] => service
[qty] => 3
[description] => Description 1
[price] => $ 33
[tax] => 60 %
[total] => $ 158.40
)
[2] => Array (
[unit] => hours
[qty] => 74
[description] => Description 2
[tax] => 3 %
[total] => $ 3,429.90 )
[3] => Array (
[unit] => days
[qty] => 67
[description] => Description 3
[tax] => 6 %
[total] => $ 284.08 )
[4] => Array (
[unit] => product
[qty] => 72
[description] => Description 4
[tax] => 34 %
[total] => $ 3,183.84
)
)
我用它作為我的每條陳述,但沒有奏效:
$returnedData = $_POST['item'];
foreach($returnedData as $data) {
$unit = $data['unit'];
$qty = $data['qty'];
$description = $data['description'];
$tax = $data['tax'];
$price = $data['price'];
$subtotal = $data['total'];
}
$insert_items = mysqli_query($connect_db,"INSERT INTO items (invoice_id, date, unit, qty, description, tax, price, subtotal) VALUES ($invoice, $todays_date, $unit, $qty, $description, $tax, $price, $subtotal)");
有人知道如何捕獲所有數據嗎? 每個循環都可以嗎?
可能會幫到您。 因為這是一個多維數組,所以您可以再應用一個循環來獲取值,並在第一個循環之后編寫INSERT查詢
$returnedData = $_POST['item'];
foreach($returnedData as $value=>$data)
{
$unit = $data['unit'];
$qty = $data['qty'];
$description = $data['description'];
$tax = $data['tax'];
$price = $data['price'];
$subtotal = $data['total'];
$insert_items = mysqli_query($connect_db,"INSERT INTO items
(invoice_id, date, unit, qty, description, tax, price, subtotal)
VALUES
($invoice, $todays_date, $unit, $qty, $description, $tax, $price, $subtotal)");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.