首页 > 解决方案 > 尝试为此 API 创建多个用户时遇到问题?

问题描述

我可以通过 Postman 发布一个用户的创建,但我无法调整此代码,以便我可以发布多个用户的创建。我试图创建一个 foreach 循环,但无法让它工作,所以我放弃了它。如果有人可以帮助我,我将非常感激。

这是 createOne.php 的代码

<?php
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");

include_once '../config/database.php';
include_once '../class/users.php';

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

$item = new users($db);

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

$item->user_id = $data->user_id;
$item->user_name = $data->user_name;
$item->age = $data->age;
$item->birthday = date('YYYY-MM-DD');
$item->gender = $data->gender;

if($item->createUser()){
    echo 'User created successfully.';
} else{
    echo 'User could not be created.';
}
?>

这是 users.php

<?php
class users{

    // Connection
    private $conn;

    // Table
    private $db_table = "users";

    // Columns
    public $user_id;
    public $user_name;
    public $age;
    public $birthday;
public $gender;


    // Db connection
    public function __construct($db){
        $this->conn = $db;
    }
    
    // CREATE
    public function createUser(){
        $sqlQuery = "INSERT INTO
                        ". $this->db_table ."
                    SET
                        user_id = :user_id, 
                        user_name = :user_name, 
                        age = :age, 
                        birthday = :birthday, 
gender = :gender";
                        

        $stmt = $this->conn->prepare($sqlQuery);

        // sanitize
        $this->user_id=htmlspecialchars(strip_tags($this->user_id));
        $this->user_name=htmlspecialchars(strip_tags($this->user_name));
        $this->age=htmlspecialchars(strip_tags($this->age));
        $this->birthday=htmlspecialchars(strip_tags($this->birthday));
        $this->gender=htmlspecialchars(strip_tags($this->gender));


        // bind data
        $stmt->bindParam(":user_id", $this->user_id);
        $stmt->bindParam(":user_name", $this->user_name);
        $stmt->bindParam(":age", $this->age);
        $stmt->bindParam(":birthday", $this->birthday);
        $stmt->bindParam(":gender", $this->gender);

        if($stmt->execute()){
            return true;
        }
        return false;
    }

}
?>


这是我尝试在 createOne 文件中执行的操作:

foreach ((array) $data as $key=>$value) {
    $item->user_id = $data->user_id;
$item->user_name = $data->user_name;
$item->age = $data->age;
$item->birthday = date('YYYY-MM-DD');
$item->gender = $data->gender;

if($item->createUser()){
echo 'User created successfully.';
} 
else{
echo 'User could not be created.';
}

}

每当我尝试通过 Postman 发布新用户时,我都会得到以下信息:

"Notice:  Trying to get property 'user_id' of non-object in ..."
"Notice:  Trying to get property 'user_name' of non-object in ..."
"Notice:  Trying to get property 'age' of non-object in ..."
"Notice:  Trying to get property 'birthday' of non-object in ..."
"Notice:  Trying to get property 'gender' of non-object in ..."
"User could not be created"

(例如:当我尝试发布 3 个新用户时,由于循环,我得到了 3 次以上的响应)

标签: phpapipostman

解决方案


推荐阅读