1

I am trying to sync an SQLite database to a MySQL server and it seems that the way to do this is to convert the SQLite database to a JSON file and then send it over to PHP to write that into the database. I have created this php file to accept the JSON data:

<?php
include 'databaseconnect.php';
$jsonInput = $_POST['json'];
echo $jsonInput;
$data = json_decode($jsonInput);
foreach($data as $inv){
    $id = $inv->id;
    $inMillis = $inv->inMillis;
    $outMillis = $inv->outMillis;
    $complete = $inc->complete;
    $sql = "INSERT INTO history (id, inMillis, outMillis, complete) VALUES ('$id', '$inMillis', '$outMillis', '$complete')";
    $res = mysqli_query($conn, $sql);
}
?>

I have also managed to convert my data on the android side to a JSON format:

[{"ID":"1","INMILLIS":"1474045680982","OUTMILLIS":"1474046580983","COMPLETE":"1"}]

I am now really struggling to send this data to the PHP file and all of the solutions I can find online don't work. I was wondering if someone could help me find a solution? Thanks!

2 Answers2

0
echo "<pre>";

$jsonInput = '[{"ID":"1","INMILLIS":"1474045680982","OUTMILLIS":"1474046580983","COMPLETE":"1"}]'; 
$data = json_decode($jsonInput,true);

foreach($data as $inv){
    $id = $inv['ID'];
    $inMillis = $inv['INMILLIS'];
    $outMillis = $inv['OUTMILLIS'];
    $complete = $inv['COMPLETE'];
    $sql = "INSERT INTO history (id, inMillis, outMillis, complete) VALUES ('$id', '$inMillis', '$outMillis', '$complete')";

    echo $sql."\n";

}

convert json in array $data = json_decode($jsonInput,true); and loop with foreach

output: INSERT INTO history (id, inMillis, outMillis, complete) VALUES ('1', '1474045680982', '1474046580983', '1')

CatalinB
  • 571
  • 4
  • 11
0

In your Android app generate an http post with the json in the body of the post.

Jason Grife
  • 1,197
  • 11
  • 14