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条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题