簡體   English   中英

如何將解碼的json數據存儲到變量中並將其插入到mysql表中

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

嗨,在我的Android應用程序中,我從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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM