2014-11-21 3 views
0

안녕하세요, 내 안드로이드 응용 프로그램에서 Iam이 sqlite에서 데이터를 가져 와서 JSON을 사용하여 해당 데이터를 mysql 테이블에 저장하고 싶습니다.디코딩 된 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 
?> 
+1

, ([이'mysql_로 *'기능을 사용하지 마십시오] 제발 http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions- in-php), 더 이상 유지 관리되지 않으며 공식적으로 사용되지 않습니다 (https://wiki.php.net/rfc/mysql_deprecation). 대신 [prepared statements] (http://en.wikipedia.org/wiki/Prepared_statement)에 대해 알아보고 [PDO] (http://us1.php.net/pdo) 또는 [MySQLi] (http : //)를 사용하십시오. us1.php.net/mysqli). [SQL 주입 방지!] (http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) –

답변

1
$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); 
} 
+0

응답도 무엇인지 알 수 있습니까? 결국? – Vandana

+0

모든 $ cycle_id 값을 배열에 모으십시오. 루프 이후에 출력하십시오. – gvk

+0

사실 내 상황에서 them.what를 수집하거나 저장해야 할 필요가 없습니다. sqlite에서 json 데이터를 작성하고 저장하면 mysql이 실행됩니다. 이제 응답을 건너 뛸 수 있습니까? – Vandana

0
$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); 
} 
+0

도움에 감사드립니다. – Vandana

관련 문제