在php中通过url传递数据时出现问题

时间:2018-07-03 13:19:27

标签: php mysql mysqli

在将数据从表单通过URL传递到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."}

在文件create.php中找到以下代码,我在提交后将数据从表单传递到数据库时遇到了问题。

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 '}';
  }

   ?>

请通过使用php将数据从表单通过url传递到数据库来帮助解决此问题。

0 个答案:

没有答案