php mysql 数组参数_在PHP MySQL数据库中插入嵌套的JSON数组作为参数

bd96500e110b49cbb3cd949968f18be7.png

I want to insert JSON parameters in my phpmyadmin database. I tried with below code but it didn't work. Kindly help me in this.

my JSON parameters are:

`$ "address" = "bharuch"

"customer_id" = "108"

"products" = "[{"product_id":"1","product_name":"Potato","category_id":"1","subcategory_id":"1","product_memberprice":"11","product_nonmemberprice":"14","product_minquantity":"500 gms","product_image":"http:\/\/gaubharat.in\/gaubharat\/images\/potato.png","product_brand":"","sub_total":"28","user_qty":"2"},{"product_id":"2","product_name":"Tomato","category_id":"1","subcategory_id":"1","product_memberprice":"15","product_nonmemberprice":"18","product_minquantity":"500 gms","product_image":"http:\/\/gaubharat.in\/gaubharat\/images\/tomato.png","product_brand":"","sub_total":"18","user_qty":"1"}]"

"pincode" = "392025"

"order_totalamount" = "46"`

and my PHP code is:

require("config.inc.php");

$address = $_POST['address'];

$customerid = $_POST['customer_id'];

$amount = $_POST['order_totalamount'];

$pincode = $_POST['pincode'];

$product = json_decode($_POST['products']);

foreach( $product as $key => $val)

{

$product_id = $val['product_id'];

$product_name = $val['product_name'];

$category_id = $val['category_id'];

$subcategory_id = $val['subcategory_id'];

$product_memberprice = $val['product_memberprice'];

$product_nonmemberprice = $val['product_nonmemberprice'];

$product_minquantity = $val['product_minquantity'];

$product_image = $val['product_image'];

$product_brand = $val['product_brand'];

$sub_total = $val['sub_total'];

$user_qty = $val['user_qty'];

$query = "INSERT INTO `order`(cm_id,product_id,product_quantity,sub_total,order_totalamount,order_id,address,pincode,order_date) VALUES ('$customerid','$product_id','$user_qty','$sub_total','$amount','1','$address','$pincode',CURDATE())";

if(!mysqli_query($db,$query))

{

die('Error : ' . mysql_error());

}

else{

$response["success"] = 1;

$response["message"] = "You order placed successfully!";

echo json_encode($response);

}

}

?>

Kindly help me out in this.

解决方案

You need to add json_decode function for $_POST['products'] as:

$address = $_POST['address'];

$customerid = $_POST['customer_id'];

$amount = $_POST['order_totalamount'];

$pincode = $_POST['pincode'];

$products = json_decode($_POST['products']);

foreach($products as $key => $val){

...

UPDATE 1:

You have an issue in your query. You are using 9 columns and insert 10 values.

$query = "INSERT INTO `order`(cm_id,product_id,product_quantity,sub_total,order_totalamount,order_id,address,pincode,order_date) VALUES ('$customerid','$product_id','$user_qty','$sub_total','$amount','1','$address',,'$pincode',NOW())";

What is this ,, after $address?

UPDATE 2:

Modified code:

require("config.inc.php");

$address = $_POST['address'];

$customerid = $_POST['customer_id'];

$amount = $_POST['order_totalamount'];

$pincode = $_POST['pincode'];

$product = json_decode($_POST['products']);

$values = array();

foreach($product as $key => $val)

{

$product_id = $val->product_id;

$product_name = $val->product_name;

$category_id = $val->category_id;

$subcategory_id = $val->subcategory_id;

$product_memberprice = $val->product_memberprice;

$product_nonmemberprice = $val->product_nonmemberprice;

$product_minquantity = $val->product_minquantity;

$product_image = $val->product_image;

$product_brand = $val->product_brand;

$sub_total = $val->sub_total;

$user_qty = $val->user_qty;

$values[] = "('$customerid','$product_id','$user_qty','$sub_total','$amount','1','$address','$pincode',CURDATE())";

}

if(count($values) > 0){

$query = "INSERT INTO `order` (cm_id,product_id,product_quantity,sub_total,order_totalamount,order_id,address,pincode,order_date) VALUES ";

$query .= implode(",",$values);

if(!mysqli_query($db,$query))

{

echo "Error";

}

else

{

$response["success"] = 1;

$response["message"] = "You order placed successfully!";

echo json_encode($response);

}

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值