gpt4 book ai didi

php - 在 php mysql 数据库中插入嵌套 JSON 数组作为参数

转载 作者:行者123 更新时间:2023-11-29 21:36:56 24 4
gpt4 key购买 nike

我想在我的 phpmyadmin 数据库中插入 JSON 参数。我尝试使用下面的代码,但没有成功。请帮助我。

我的 JSON 参数是:

 `$ "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"`

我的 PHP 代码是:

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

请帮我解决这个问题。

最佳答案

您需要为$_POST['products']添加json_decode函数,如下:

$address = $_POST['address'];
$customerid = $_POST['customer_id'];
$amount = $_POST['order_totalamount'];
$pincode = $_POST['pincode'];
$products = json_decode($_POST['products']);
foreach($products as $key => $val){
...

更新1:

您的查询有问题。您正在使用 9 列并插入 10 个值。

$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())";

$address 后面的,,是什么?

更新2:

修改后的代码:

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

关于php - 在 php mysql 数据库中插入嵌套 JSON 数组作为参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34848563/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com