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


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

嗨,在我的安卓应用程序中,我正在从sqlite检索数据,现在通过使用JSON,我想将该数据存储到mysql表中。请帮助我实现这一目标

我不知道如何将所有值存储在一个表中,每次尝试插入时只插入一行。

这是代码。

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