邮递员: 未捕获的 PDOException: SQLSTATE[HY000]: 一般错误: 1366 不正确的整数值:



我正在处理 youtube 上 TraversyMedia 频道的 PHP REST API FROM SCRATCH,其中邮递员用于检查一切是否正常工作。

包含代码的存储库在这里

在邮递员中,我收到以下错误,不知道如何解决。

注意:尝试获取非对象的属性"标题"/var/www/html/php_rest_myblog/api/post/create.php 在第 21 行

注意:尝试获取非对象的属性"主体"/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} 在/var/www/html/php_rest_myblog/models/Post.php 第 91 行抛出

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

从创建.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')
);
}

默认情况下,bindValue(( 采用字符串数据类型。如果要使用 INT 值,则需要在调用中指定该值


$stmt->bindParam(':category_id', $this->category_id, PDO::PARAM_INT);

最新更新