简体   繁体   English

在codeigniter中使用php在mysql数据库中插入json

[英]Insert json in mysql database using php in codeigniter

I am getting this response from an android app in JSON, I want to fetch the response and insert each data as a single row below down is my JSON response model and controller thanks in advance我从 JSON 中的 android 应用程序收到此响应,我想获取响应并将每个数据插入为下面的单行是我的 JSON 响应模型和控制器,提前致谢

Response回复

{
    "DEALS": [{
        "ORDER_DATE": "2020-09-06 03:31:23 PM",
        "CUSTOMER_ID": "umersaleem_03334033313",
        "QUANTITY": 1,
        "DEAL_ID": "3",
        "ORDER_TOTAL": "2100.0"
    }, {
        "ORDER_DATE": "2020-09-06 03:31:23 PM",
        "CUSTOMER_ID": "umersaleem_03334033313",
        "QUANTITY": 1,
        "DEAL_ID": "3",
        "ORDER_TOTAL": "2100.0"
    }]
}

controller控制器

        $reponse=array();
 $this->db->trans_begin();
    foreach($data as $row) {
        $filter_data = array(
            'ORDER_DATE'=> $order_date,
            'CUSTOMER_ID' => $customer_id,
            'PRODUCT_ID' => $product_id,
            'QUANTITY' => $quantity,
            'DEAL_ID' => $deal_id,
            'ORDER_TOTAL' => $order_total,
        );
       //Call the save method
       $this->Order_model->insertorderinfo($filter_data);
    }

    if ($this->db->trans_status() === FALSE) {
        $this->db->trans_rollback();
        echo json_encode("Failed to Save Data");
    } else {
        $this->db->trans_commit();
        echo json_encode("Success!");
    }

Model模型

public function insertorderinfo($data) {
    $this->db->insert('ORDER_INFO', $data);
}

Try This尝试这个

<?php
$test_json='{"DEALS":[{"ORDER_DATE":"2020-09-06 03:31:23 PM","CUSTOMER_ID":"umersaleem_03334033313","QUANTITY":1,"DEAL_ID":"3","ORDER_TOTAL":"2100.0"},{"ORDER_DATE":"2020-09-06 03:31:23 PM","CUSTOMER_ID":"umersaleem_03334033313","QUANTITY":1,"DEAL_ID":"3","ORDER_TOTAL":"2100.0"}]}';

$json=(json_decode($test_json, true));

foreach ($json as $key => $value) {
    // get count as in key there is deals
    $total_order=count($value);
    for($i=0; $i<$total_order; $i++){
        echo $value[$i]['ORDER_DATE']."<br />";
        echo $value[$i]['CUSTOMER_ID']."<br />";
        echo $value[$i]['QUANTITY']."<br />";
        echo $value[$i]['DEAL_ID']."<br />";
        echo $value[$i]['ORDER_TOTAL']."<br />";
    }
}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用php将Json数据插入具有json数据类型的mysql数据库中 - Insert Json data into mysql database with json datatype using php 使用PHP和JSON从XCode将图像数据插入MySQL数据库 - Insert imagedata into MySQL database from XCode using PHP & JSON 使用PHP将代码html插入MYSQL(Codeigniter) - Insert tag html into MYSQL using PHP (Codeigniter) 如何使用AJAX,PHP和CodeIgniter插入数据库? - How to insert into database using AJAX, PHP and CodeIgniter? 使用codeigniter api从postman插入多个json数据到mysql数据库 - Insert multiple json data into mysql database from postman using codeigniter api PHP MySQL 使用 Codeigniter-3 + Dropzone-4.1 使用 $_GET 方法插入表数据库 - PHP MySQL insert into table database with $_GET method using Codeigniter-3 + Dropzone-4.1 使用PHP在MySQL数据库中插入JSON数据 - Insert JSON data in MySQL database with PHP 我如何在php MySQL数据库中使用json字符串插入解码的json数据 - How can i insert decoded json data using json string in php mysql database 如何使用Codeigniter将具有不同值的输入插入mysql数据库? - How to insert an input with different values in to the mysql database using Codeigniter? 如何在codeigniter中使用ajax在mysql数据库中插入数据? - How To insert data in mysql database using ajax in codeigniter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM