Php 试图获取非对象错误的属性,而我应该获取一个对象?

Php 试图获取非对象错误的属性,而我应该获取一个对象?,php,rest,postman,Php,Rest,Postman,我遇到了一些错误,比如:试图在第24行的D:\ShiroWorks\restapi authentication example\api\create_user.php中获取非对象的属性“firstname”,试图用postman发布数据 我尝试使用file_get_contents函数作为数组,但收到消息无法创建用户 header("Access-Control-Allow-Origin: http://localhost/rest-api-authentication-example/");

我遇到了一些错误,比如:试图在第24行的D:\ShiroWorks\restapi authentication example\api\create_user.php中获取非对象的属性“firstname”,试图用postman发布数据

我尝试使用file_get_contents函数作为数组,但收到消息无法创建用户

header("Access-Control-Allow-Origin: http://localhost/rest-api-authentication-example/");
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");


include_once 'config/database.php';
include_once 'objects/user.php';


$database = new Database();
$db = $database->getConnection();


$user = new User($db);


$data = json_decode(file_get_contents("php://input"));


 $user->firstname = $data->firstname;
 $user->lastname = $data->lastname;
 $user->email = $data->email;
 $user->password = $data->password;



// create the user
if(
    !empty($user->firstname) &&
    !empty($user->email) &&
    !empty($user->password) &&
    $user->create()
){


    http_response_code(200);

    // display message: user was created
    echo json_encode(array("message" => "User was created."));
}

// message if unable to create user
else{


    http_response_code(400);

    // display message: unable to create user
    echo json_encode(array("message" => "Unable to create user."));
}
?>
// The result of var_dump($data) is: 
array(4) {
  [
    "firstname"
  ]=>
  string(6) "Vilmos"
  [
    "lastname"
  ]=>
  string(5) "Szabó"
  [
    "email"
  ]=>
  string(15) "shiro@email.com"
  [
    "password"
  ]=>
  string(3) "555"
}


我应该返回消息:用户已创建。

错误是因为数据库。还有一行被设置为NOTNULL,我没有将其包含在post请求中。

我们需要邮递员数据来回答这个问题。在“邮递员”中,当您处于此请求的选项卡上时,单击“发送”按钮下方Cookie和comments旁边的代码超链接。从下拉列表中选择CURL并将该代码粘贴到此处作为问题的一部分。您的PHP代码看起来不错,所以我怀疑您的请求格式不正确。默认情况下,json_decode返回object,但您的var_dump将类型显示为数组。它是如何发生的?您可以运行var_dump$user;在注释之前//创建用户?``objectUser3 7{[conn:user:private]=>objectPDO2 0{}[table_name:user:private]=>string5用户[id]=>NULL[firstname]=>string6 Vilmos[lastname]=>string6 Szabó[email]=>string15shiro@email.com[密码]=>string3 555}“邮递员数据太长,所以我将把它分为两部分。”curl-X POST\\-H'Accept:/'\-H'Accept Encoding:gzip,deflate'\-H'缓存控制:无缓存'\-H'连接:保持活动'\-H'内容长度:119'\-H'内容类型:text/plain'\-H'Cookie:PHPSESSID=97hhhkrlai3apbcg3g2h9rcj5i7'\-H'主机:8888'\-H'邮差令牌:bbb45f60-9e7b-4b7d-989b-fe864a872eca,b0b9ffc7-4d75-4fa6-9e11-eee99d21f896'\``我在同一教程中发现了相同的问题,可以确认SQL字段“created”和“modified”在创建数据库表时要求默认值为null。引发此错误的原因是,将用户写入用户表的SQL语句中未包含日期字段。只需将表日期字段设置为允许空值即可解决此问题。