dsio68964998
2014-11-21 22:06
浏览 118

如何将已解码的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
?>

图片转代码服务由CSDN问答提供 功能建议

您好我的Android应用程序Iam从sqlite检索数据,现在使用JSON我希望将该数据存储到mysql中 table.Please帮我实现这个目标

我没有得到如何在表中存储所有值,每次尝试插入时都会插入一行。 \ n

这是代码。

 &lt;?php 
 
error_reporting(0); 
 
include_once'db_conn.php';  //包含数据库连接字符串
 
 
 $ received_json = $ _POST [“sparesJSON”]; 
 
if(get_magic_quotes_gpc())
 
 {
 $ received_json = stripslashes($ received_json); \  n} 
 
 $ received_json = json_decode($ received_json); 
 
 //捕获变量
 
 $ item_name = $ received_json [0]  - &gt; item_name; 
 
 $ quantity = $ received_json [  0]  - &gt;数量; 
 
 $ total_price = $ received_json [0]  - &gt; total_price; 
 
 $ cycle_id = $ received_json [0]  - &gt; cycle_id; 
 
 $ date = $ received_json  [0]  - &gt; date; 
 
for($ i = 0; i&lt; count($ received_json; i ++)
 {
 
 
 $ insert_spares =“插入spares_items(item_name,quantity,total_price,  cycle_id,date)values(\“$ item_name \”,\“$ quantity \”,\“$ total_price \”,\“$ cycle_id \”,\“$ date \”)“; 
 
 mysql_query($  insert_spares); 
} 
 
 //在json 
 
echo中编码结果数组json_encode($ cycle_id); 
 
 
 //将此作为对Android 
?&gt; 
的响应发送   
 
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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);
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • duanjiaren8188 2014-11-21 22:37
    $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);
    }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题