如何从Android Json Request中获取PHP中的Json数据并将其存储在MySql数据库中

时间:2018-08-13 07:58:24

标签: arrays json phpmyadmin http-post jsonobjectrequest

我使用PHP发送一些数据JsonArray作为POST请求。但是我无法接收这些数据并且无法将其存储为MySQL数据库。怎么可能?需要解决方案吗?

  1. 如何在php中获取Json请求?
  2. 从android发送JSON数组
  3. 以下样品请求

示例android发送json发送请求

{  
   "data":[  
      {  
         "mobile_no":"0172",`enter code here`
         "earn_date":"2018-08-12",
         "earncoin":10,
         "totalcoin":16,
         "bonuscoin":0,
         "paymentrequest":0
      },
      {  
         "mobile_no":"017256",
         "earn_date":"2018-08-13",
         "earncoin":3,
         "totalcoin":167,
         "bonuscoin":0,
         "paymentrequest":1
      }
   ]
}

如何在PHP上接收此json并将其插入到mysql数据库中。代码示例(但不起作用)..什么是错误,需要解决方案

  <?php
    if($_SERVER['REQUEST_METHOD']=='POST'){

    include 'DatabaseConfig.php';

    $con = mysqli_connect($HostName,$HostUser,$HostPass,$DatabaseName);

     $json = file_get_contents('php://input');
     $data = json_decode($json, true);
     //echo $data['data'][0]['mobile_no']; 
      $obj['data']=array();
    //$JSON_Received = $_POST["data"];
     //$obj = json_decode($_POST["data"],true);


    if (is_array($obj) || is_object($obj)) {
      for($i=0; i<$obj.length; $i++){
     //foreach ($obj as $objc) {

     $mobile_no= $obj['data'][$i]['mobile_no'];
     $earn_date= $obj['data'][$i]['earn_date'];
     $earncoin= $obj['data'][$i]['earncoin'];
     $totalcoin= $obj['data'][$i]['totalcoin'];
     $bonuscoin= $obj['data'][$i]['bonuscoin'];
     $paymentrequest= $obj['data'][$i]['paymentrequest'];


    $Sql_Query = "INSERT INTO transhitory (mobile_no,earn_date,earncoin,totalcoin,bonuscoin,paymentrequest) values ('$mobile_no','$earn_date','$earncoin','$totalcoin','$bonuscoin','$paymentrequest')";

     if(mysqli_query($con,$Sql_Query))
    {
     echo 'Data Inserted Successfully';
    }


    else
    {
     echo 'Data Inserted Failed';
     }



     if($paymentrequest!=0){

     $Sql_Query_finaltable = "INSERT INTO lastpayment (mobile_no,lastpayment_date,last_paymentcoin,last_paymenttk) values ('$mobile_no','0','$totalcoin','0')";

     if(mysqli_query($con,$Sql_Query_finaltable ))
    {
     echo 'Data Inserted Successfully on Review';
    }
    else
    {
     echo 'Data Inserted Failed on Review';
     }
     }else{ 
    }
    }
    //else{
    //echo 'not reach foreach condition';
    //}
    }else{
     echo 'Json array na Obj confused.. not reached if conditon';
    }
    }

     mysqli_close($con);
    ?>

0 个答案:

没有答案