Click here to Skip to main content
15,851,335 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,


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

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

if (!$con)

echo mysql_error($con);

//end make connections

//connect to database

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


echo mysqli_error($con);



foreach ( $data as $item )

//variable $item is used for iteration to with the $con for database connection

foreach($item->KYCInfo as $kycinfo)


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



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


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' )";




What I have tried:

This is the json format that is to be consumed

  "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\"}]"
Updated 28-Aug-17 9: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