0

I have my ubuntu 18.04 server with apache2. I am trying to insert data from json file into my mysql database using curl POST. Curl will invokes php script (my-parser.php) passing json file. The php script will parse json, create a sql query and insert the data into db.

Now Curl returns error (below). Any help will be appreciate


My Curl:

curl -vv --header 'Content-Type: application/json; charset=UTF-8' --data 
@file_with_json_data http://localhost/php-parser.php

Curl response:

Trying 127.0.0.1
Connected to localhost port 80
POST /php-parser.php HTTP/1.1
HOST:localhost
User-Agent: curl/7.58.0
Accept: */*
Content-Type: application/json; charset=UTF-8
Content-Length: 210

*upload completely sent off: 210 of 210 bytes
*HTTP/1.0 assume close after body
HTTP/1.0 500 Internal Server Error
Server: Apache/2.4.29 (Ubuntu)
Content-Length:0
Connection: close
Content-Type: text/html; charset=UTF-8

I have tried to find answer on stackoverflow but could not find the solution.

My MySQL database is My_DB, my table in it is My_Data_Table. The table has 4 columns:

index (auto_increment)
mcc TEXT, 
mcc TEXT, 
lac TEXT

My file_with_json_data:

{
"event": "some_event:,
"data": {
"MCC":"111",
"MNC":"123",
"LAC":"345
},
"num1":60,
"string":"sometext",
"num2:"123"
}

My php-parser.php:

<?php

$json=$_POST;
$data=json_decode($json, true);

$hostname="localhost";
$username="user";
$password="password";
$db="My_DB";

$dbconnect=mysqli_connect($hostname, $username, $password, $db);

if ($dbconnect->connect_error) {
    die("Database connection failed:" . $dbconnect->connect_error);
}

$mcc=$data["MCC"];
$mnc=$data["MNC"];
$lac=$data["LAC"];

$query='INSERT INTO My_Data_Table (mcc, mnc, lac)
                            VALUE ("$mcc", "$mnc", "$lac")';

if (!mysqli_query($dbconnect, $query)){
    die('An error occurred.');
}else{
    echo "Success!";
}

mysqli_close($dbconnect);

?>

I expect that curl will invoke php script which inserts the json data into mysql table in specified columns. Curl verbose reply shows

Server Error 500 HTTP/1.0 assume close after body

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • **Error checking** but if you cannot be bothered, Add `ini_set('display_errors', 1); ini_set('log_errors',1); error_reporting(E_ALL); mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` to the top of your script. This will force any mysqli_ errors to generate an Exception that you can see on the browser as well as normal PHP errors. – RiggsFolly Jun 06 '19 at 10:56

1 Answers1

0

Not sure if this will fix your 500 error, but try the following:

mySQL format is INSERT INTO table (column) VALUES (value) and mySQL expects values to be enclosed in single quotes. Try changing your SQL query to the following:

$query="INSERT INTO My_Data_Table (mcc, mnc, lac)
                            VALUES ('{$mcc}', '{$mnc}', '{$lac}')";

I'm ignoring the issues around not using prepared statements, I'm sure you'll get a ton of warnings from other users about that.

dearsina
  • 4,774
  • 2
  • 28
  • 34