php - 尝试为此 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 次以上的响应)
解决方案
推荐阅读
- java - 无法比较不同时区的字符串日期
- r - 运行代码 1000 次并将其保存在 R 向量中
- swift - 协议触发时未调用 ObservedObject 类函数
- sequelize.js - 我可以直接在 Nestjs 控制器中访问续集模型吗?
- apache-nifi - 是否可以在 nifi 中每 5 分钟从 Web 服务获取详细信息
- javascript - Google Apps 脚本 - 使用 isChecked() 的非常基本的循环问题
- git - 当检测到冲突导致文件损坏时,Git(通过 TortoiseGit)修改编译文件
- reactjs - tsconfig 路径和 webpack 别名 - 找不到模块问题
- mysql - mysql数据库中的日期与我得到的响应日期不同(使用SpringBoot)
- windows - 为 Windows 10 设置安装以指定不带空格的驱动器号和路径