I have been attempting to send my data from my app to my local server via php. I am able to parse my data through and serialised it into JSON and supposedly send it to the server like
this question will demonstrate. However when I test it on POSTMAN, the $array says that it is NULL
and the $json says string(0)""
. I am very confused as to why this is the case.
This is how my data is structured when parsed:
Optional([["record_id": 8EC9C1C9-7DD4-4343-B7CC-E4615FDDA150, "name": John ], ["record_id": 7EEA551D-9432-4737-99FB-6BFCF3A92D21, "name": Fred Smith]])
Below is my php file:
<?php
ini_set("display_errors", 1);
ini_set("html_errors", 1);
error_reporting(E_ALL);
$json = file_get_contents('php://input');
var_dump($json);
// convert to array
$array = json_decode($json, true);
var_dump($array);
$response = array();
//check if request is post
if($_SERVER['REQUEST_METHOD']=='POST'){
//assign values to variables
$recordID = $array['record_id'];
$name = $array['name'];
//including the db operation file
require_once '../includes/DbOperation.php';
$db = new DbOperation();
//inserting values
if($db->createTeam($recordID, $name)){
$response['error']=false;
$response['message']='Record added successfully';
}else{
$response['error']=true;
$response['message']='Could not add record';
}
}else{
$response['error']=true;
$response['message']='You are not authorized';
}
echo json_encode($response);
and here is my DBOperations class:
<?php
class DbOperation
{
private $conn;
//Constructor
function __construct()
{
require_once dirname(__FILE__) . '/Config.php';
require_once dirname(__FILE__) . '/DbConnect.php';
// opening db connection
$db = new DbConnect();
$this->conn = $db->connect();
}
//Function to create a new user
public function createTeam($recordID, $name)
{
$stmt = $this->conn->prepare('INSERT INTO record(record_id, name) VALUES (?, ?)');
$stmt->bind_param("si", $recordID, $name);
$result = $stmt->execute();
$stmt->close();
if ($result) {
return true;
} else {
return false;
}
}
}
I am really struggling to resolve this. Please let me know what is the best way to handle this.