dsio68964998 2014-11-21 22:06
浏览 149
已采纳

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

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

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
?>
  • 写回答

2条回答 默认 最新

  • dtjzpg5313 2014-11-21 22:11
    关注
    $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);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。