0

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.

Derek Pollard
  • 6,953
  • 6
  • 39
  • 59
sharmila
  • 175
  • 2
  • 19
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Loek Jul 03 '18 at 13:19
  • 3
    It says that `$data` is not an object. Why do you believe that `"php://input"` contains a JSON string? Have you tried examining what `$data` is after setting it? – DFriend Jul 03 '18 at 13:24

0 Answers0