简体   繁体   English

如何将解码的json数据存储到变量中并将其插入到mysql表中

[英]How to Store decoded json data into variables and insert them in to mysql table

Hi In my android application Iam retrieving the data from sqlite and now by using JSON i want to store that data into mysql table.Please help me to achieve this 嗨,在我的Android应用程序中,我从sqlite检索数据,现在通过使用JSON将数据存储到mysql表中。请帮助我实现这一目标

I'm not getting how to store all the values in a table only single row is inserting each time I try to insert. 我没有得到如何将所有值存储在表中的信息,每次尝试插入时都只插入一行。

This is the code. 这是代码。

<?php

error_reporting(0);

include_once 'db_conn.php';  //Include the database connection strings


$received_json = $_POST["sparesJSON"];

if (get_magic_quotes_gpc())

{
    $received_json = stripslashes($received_json);
}

$received_json = json_decode($received_json);

//catch variable

$item_name = $received_json[0]->item_name;

$quantity = $received_json[0]->quantity;

$total_price = $received_json[0]->total_price;

$cycle_id = $received_json[0]->cycle_id;

$date = $received_json[0]->date;

for($i=0;i<count($received_json;i++)
{


            $insert_spares = "insert into spares_items (item_name, quantity, total_price,    cycle_id, date) values (\"$item_name\", \"$quantity\", \"$total_price\", \"$cycle_id\", \"$date\")";

            mysql_query($insert_spares);
}

//encode result array in json

echo json_encode($cycle_id);


//send this as an response to the Android
?>
$received_json = json_decode($received_json);



for($i=0;$i<count($received_json);$i++)
{


    $item_name = $received_json[$i]->item_name;

    $quantity = $received_json[$i]->quantity;

    $total_price = $received_json[$i]->total_price;

    $cycle_id = $received_json[$i]->cycle_id;

    $date = $received_json[$i]->date;    
    $insert_spares = "insert into spares_items (item_name, quantity, total_price,    cycle_id, date) values (\"$item_name\", \"$quantity\", \"$total_price\", \"$cycle_id\", \"$date\")";

    mysql_query($insert_spares);
}
$received_json = json_decode($received_json);

$inserted_names = array();

for($i=0;$i<count($received_json);$i++)
{


    $item_name = $received_json[$i]->item_name;
    // if we have already inserted this name, don't insert again
    if (in_array($item_name, $inserted_names)) {
        continue;
    }

    $quantity = $received_json[$i]->quantity;

    $total_price = $received_json[$i]->total_price;

    $cycle_id = $received_json[$i]->cycle_id;

    $date = $received_json[$i]->date;    
    $insert_spares = "insert into spares_items (item_name, quantity, total_price,    cycle_id, date) values (\"$item_name\", \"$quantity\", \"$total_price\", \"$cycle_id\", \"$date\")";

    mysql_query($insert_spares);
    // save item name to list of names already inserted
    array_push($inserted_names, $item_name);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM