1

I need to parse the json string from an android application and insert it into mysql database using php

I tried to parse json from android using "$jsonData = file_get_contents('php://input');"

my code

include 'dbconnect.php';

if($_SERVER['HTTP_ORIGIN'] == "http://www.my-domain.com") {
    header('Access-Control-Allow-Origin: http://www.my-domain.com');
    header('Content-type: application/json');
    header('Content-Type: application/x-www-form-urlencoded');
    }
   header('Access-Control-Allow-Origin: *');

   header('Access-Control-Allow-Methods: GET, POST');

   header("Access-Control-Allow-Headers: X-Requested-With");

   $jsonData = file_get_contents('php://input');
   $detail = json_decode($jsonData);



$cur_date       = date('Y-m-d');
$title       = $detail->title;
$description = $detail->description;
$deviceId    = $detail->deviceid;
$companyId   = $detail->companyId;

$sql = mysqli_query($db, "INSERT INTO notes (deviceId, companyId, title, description, createdDate) VALUES ('$deviceId', '$companyId', '$title', '$description', '$cur_date')");

if($sql)
{
    echo json_encode("Success");
}
else
{
    echo json_encode("Error!!");
}

Need to insert the values to database

Linu S
  • 225
  • 1
  • 14
  • I got the answer – Linu S Jul 19 '19 at 17:33
  • 1
    $str_data = file_get_contents('php://input'); $data = json_decode($str_data,true); $cur_date = date('Y-m-d'); $title = $data["note"]["title"]; $description = $data["note"]["description"]; $deviceId = $data["note"]["deviceid"]; $companyId = $data["note"]["companyId"]; – Linu S Jul 19 '19 at 17:33

0 Answers0