Getting the below error while passing data from form through url to database in php
<b>Notice</b>: Trying to get property 'name' of non-object in
<b>C:\xampp\htdocs\api\product\create.php</b> on line <b>24</b><br />
<b>Notice</b>: Trying to get property 'price' of non-object in
<b>C:\xampp\htdocs\api\product\create.php</b> on line <b>25</b><br />
<br />
<b>Notice</b>: Trying to get property 'description' of non-object in
<b>C:\xampp\htdocs\api\product\create.php</b> on line <b>26</b><br />
<br />
<b>Notice</b>: Trying to get property 'category_id' of non-object in
<b>C:\xampp\htdocs\api\product\create.php</b> on line <b>27</b><br />
{"message": "Product was created."}
Find below code from the file create.php where I face issue while passing the data to the database from form after submission.
create.php:
<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-
Headers, Authorization, X-Requested-With");
// get database connection
include_once '../config/database.php';
// instantiate product object
include_once '../objects/product.php';
$database = new Database();
$db = $database->getConnection();
$product = new Product($db);
// get posted data
$data = json_decode(file_get_contents("php://input"));
// set product property values
$product->name = $data->name;
$product->price = $data->price;
$product->description = $data->description;
$product->category_id = $data->category_id;
$product->created = date('Y-m-d H:i:s');
// create the product
if($product->create()){
echo '{';
echo '"message": "Product was created."';
echo '}';
}
// if unable to create the product, tell the user
else{
echo '{';
echo '"message": "Unable to create product."';
echo '}';
}
?>
Kindly help to solve this issue by passing the data from form through url to database using php.