我要从youtube的TraversyMedia频道开始使用Scratch的PHP REST API,在其中使用邮递员检查一切是否正常工作。
包含代码的存储库为here
在邮递员中,我收到以下错误,并且不知道如何解决。
注意:尝试获取非对象的属性“ title” /var/www/html/php_rest_myblog/api/post/create.php,第21行
注意:尝试获取非对象的属性“ body” /var/www/html/php_rest_myblog/api/post/create.php,第22行
注意:尝试获取非对象的属性“作者” /var/www/html/php_rest_myblog/api/post/create.php,第23行
注意:尝试获取非对象的属性“ category_id” /var/www/html/php_rest_myblog/api/post/create.php,第24行
致命错误:未捕获的PDOException:SQLSTATE [HY000]:常规错误: 1366不正确的整数值:''中的第1行的列'category_id' /var/www/html/php_rest_myblog/models/Post.php:91堆栈跟踪:#0 /var/www/html/php_rest_myblog/models/Post.php(91): PDOStatement-> execute()#1 /var/www/html/php_rest_myblog/api/post/create.php(27):Post-> create()
2 {main}在第91行的/var/www/html/php_rest_myblog/models/Post.php中抛出
post.php中的代码如下:
<?php
class Post {
// DB stuff
private $conn;
private $table = 'posts';
// Post Properties
public $id;
public $category_id;
public $category_name;
public $title;
public $body;
public $author;
public $created_at;
// Constructor with DB
public function __construct($db) {
$this->conn = $db;
}
// Get Posts
public function read() {
// Create query
$query = 'SELECT c.name as category_name, p.id, p.category_id, p.title, p.body, p.author, p.created_at
FROM ' . $this->table . ' p
LEFT JOIN
categories c ON p.category_id = c.id
ORDER BY
p.created_at DESC';
// Prepare statement
$stmt = $this->conn->prepare($query);
// Execute query
$stmt->execute();
return $stmt;
}
// Get Single Post
public function read_single() {
// Create query
$query = 'SELECT c.name as category_name, p.id, p.category_id, p.title, p.body, p.author, p.created_at
FROM ' . $this->table . ' p
LEFT JOIN
categories c ON p.category_id = c.id
WHERE
p.id = ?
LIMIT 0,1';
// Prepare statement
$stmt = $this->conn->prepare($query);
// Bind ID
$stmt->bindParam(1, $this->id);
// Execute query
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
// Set properties
$this->title = $row['title'];
$this->body = $row['body'];
$this->author = $row['author'];
$this->category_id = $row['category_id'];
$this->category_name = $row['category_name'];
}
// Create Post
public function create() {
// Create query
$query = 'INSERT INTO ' . $this->table . ' SET title = :title, body = :body, author = :author, category_id = :category_id';
// Prepare statement
$stmt = $this->conn->prepare($query);
// Clean data
$this->title = htmlspecialchars(strip_tags($this->title));
$this->body = htmlspecialchars(strip_tags($this->body));
$this->author = htmlspecialchars(strip_tags($this->author));
$this->category_id = htmlspecialchars(strip_tags($this->category_id));
// Bind data
$stmt->bindParam(':title', $this->title);
$stmt->bindParam(':body', $this->body);
$stmt->bindParam(':author', $this->author);
$stmt->bindParam(':category_id', $this->category_id);
// Execute query
if($stmt->execute()) {
return true;
}
// Print error if something goes wrong
printf("Error: %s.\n", $stmt->error);
return false;
}
// Update Post
public function update() {
// Create query
$query = 'UPDATE ' . $this->table . '
SET title = :title, body = :body, author = :author, category_id = :category_id
WHERE id = :id';
// Prepare statement
$stmt = $this->conn->prepare($query);
// Clean data
$this->title = htmlspecialchars(strip_tags($this->title));
$this->body = htmlspecialchars(strip_tags($this->body));
$this->author = htmlspecialchars(strip_tags($this->author));
$this->category_id = htmlspecialchars(strip_tags($this->category_id));
$this->id = htmlspecialchars(strip_tags($this->id));
// Bind data
$stmt->bindParam(':title', $this->title);
$stmt->bindParam(':body', $this->body);
$stmt->bindParam(':author', $this->author);
$stmt->bindParam(':category_id', $this->category_id);
$stmt->bindParam(':id', $this->id);
// Execute query
if($stmt->execute()) {
return true;
}
// Print error if something goes wrong
printf("Error: %s.\n", $stmt->error);
return false;
}
// Delete Post
public function delete() {
// Create query
$query = 'DELETE FROM ' . $this->table . ' WHERE id = :id';
// Prepare statement
$stmt = $this->conn->prepare($query);
// Clean data
$this->id = htmlspecialchars(strip_tags($this->id));
// Bind data
$stmt->bindParam(':id', $this->id);
// Execute query
if($stmt->execute()) {
return true;
}
// Print error if something goes wrong
printf("Error: %s.\n", $stmt->error);
return false;
}
}
,然后在create.php中,代码如下:
<?php
// Headers
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Allow-Headers: Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
include_once '../../config/Database.php';
include_once '../../models/Post.php';
// Instantiate DB & connect
$database = new Database();
$db = $database->connect();
// Instantiate blog post object
$post = new Post($db);
// Get raw posted data
$data = json_decode(file_get_contents("php://input"));
$post->title = $data->title;
$post->body = $data->body;
$post->author = $data->author;
$post->category_id = $data->category_id;
// Create post
if($post->create()) {
echo json_encode(
array('message' => 'Post Created')
);
} else {
echo json_encode(
array('message' => 'Post Not Created')
);
}
答案 0 :(得分:1)
默认情况下,bindValue()假定为字符串数据类型。如果要使用INT值,则需要在调用中指定该值
$stmt->bindParam(':category_id', $this->category_id, PDO::PARAM_INT);