Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greetings, have been working on the below code. am consuming json in php, i want to get the data and store in a database, kindly am a beginner and need some help. the below code does not store any data.

I have been going through, it seems there is something to adding up,



PHP
<?php

if($_SERVER["REQUEST_METHOD"] == "POST" && $_SERVER["CONTENT_TYPE"] == "application/json")
{
  $json = file_get_contents("php://input", false, stream_context_get_default(), 0, $_SERVER["CONTENT_LENGTH"]);
  global $_POST_JSON;
  $_POST_JSON = json_decode($_REQUEST["JSON_RAW"]);
  
  // merge JSON-Content to $_REQUEST 
  if(is_array($_POST_JSON)) $_REQUEST   = $_POST_JSON+$_REQUEST;
}




//variables for database credentials
$host="localhost";
$username="myusername";
$password="pass";
$database="mydb";

$con = mysql_connect($host,$username,$password);

if (!$con)

{
echo mysql_error($con);
exit();
}

//end make connections

//connect to database

if (!mysql_select_db($con,$database ))

{

echo mysqli_error($con);

exit();

}


foreach ( $data as $item )

{
//variable $item is used for iteration to with the $con for database connection
$transactiontype=mysqli_real_escape_string($con,$item->TransType);
$transid=mysqli_real_escape_string($con,$item->TransID);
$transtime=mysqli_real_escape_string($con,$item->TransTime);
$transamount=mysqli_real_escape_string($con,$item->TransAmount);
$businessshortcode=mysqli_real_escape_string($con,$item->BusinessShortCode);
$billrefno=mysqli_real_escape_string($con,trim($item->BillRefNumber));
$invoiceno=mysqli_real_escape_string($con,$item->InvoiceNumber);
$msisdn=mysqli_real_escape_string($con,$item->MSISDN);
$orgaccountbalance=mysqli_real_escape_string($con,$item->OrgAccountBalance);

foreach($item->KYCInfo as $kycinfo)

{

if ($kycinfo->KYCName=='[Personal Details][First Name]' )

{
$firstname=mysqli_real_escape_string($con,$kycinfo->KYCValue);

}

if ($kycinfo->KYCName=='[Personal Details][Middle Name]' )

{
$middlename=mysqli_real_escape_string($con,$kycinfo->KYCValue);
}

if ($kycinfo->KYCName=='[Personal Details][Last Name]' )

{
$lastname=mysqli_real_escape_string($con, $kycinfo->KYCValue);
}

}
mysql_select_db($con,$database );
$sql="INSERT INTO orocoke(
TransactionType, TransID, TransTime, TransAmount, BusinessShortCode, BillRefNumber, InvoiceNumber, MSISDN, First_Name, Middle_Name,
Last_Name, OrgAccountBalance)

VALUES ('$transactiontype','$transid', '$transtime', '$transamount', '$businessshortcode', '$billrefno', '$invoiceno', '$msisdn', '$firstname',
'$middlename', '$lastname', '$orgaccountbalance' )";

mysql_query($sql);






mysql_close($con);
}

?>


What I have tried:

This is the json format that is to be consumed

JSON
{ 
  "BusinessShortCode": "2014022",
  "Trans_Time": "20140227082020",
  "Trans_Amount": "123.00",
  "Trans_Type": "PayBill",
  "MSISDN": "254722703614",
   "BillRefNumber": "vnascb90",
  "OrgAccountBalance": "vnascb90",
  "InvoiceNumber": "vnascb90",
  "Account_Number": "12345556",
  "KYCInfo": "[{\"KYCName\": \"[Personal Details][First Name]\", \"KYCValue\": \"Hoiyor\"}, {\"KYCName\": \"[Personal Details][Middle Name]\", \"KYCValue\": \"G\"}, {\"KYCName\": \"[Personal Details][Last Name]\", \"KYCValue\": \"Chen\"}]"
}
Posted
Updated 28-Aug-17 8:27am

1 solution

Break down your problem into pieces to find out where it's failing.

For Example:

1) Did you try to echo the value of $sql to see what it is you're trying to send to the server?

2) If that's got problems, like all the (values are empty), did you try something like print_r() for the parsed JSON?

3) Is your connection to the server working?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900