php - 邮递员:未捕获的 PDOException:SQLSTATE [HY000]:一般错误:1366 不正确的整数值:''
问题描述
我正在 youtube 上的 TraversyMedia 频道从零开始编写 PHP REST API,其中邮递员用于检查一切是否正常工作。
带有代码的存储库在这里
在 Postman 中,我收到以下错误,但不知道如何解决。
注意:尝试在第 21 行的 /var/www/html/php_rest_myblog/api/post/create.php 中获取非对象的属性“标题”
注意:尝试在第 22 行的 /var/www/html/php_rest_myblog/api/post/create.php 中获取非对象的属性“主体”
注意:尝试在第 23 行的 /var/www/html/php_rest_myblog/api/post/create.php 中获取非对象的属性“作者”
注意:尝试在第 24 行的 /var/www/html/php_rest_myblog/api/post/create.php 中获取非对象的属性 'category_id'
致命错误:未捕获的 PDOException:SQLSTATE [HY000]:一般错误:1366 不正确的整数值:'' for column 'category_id' at row 1 in /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->创造()
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')
);
}
解决方案
默认情况下,bindValue() 采用字符串数据类型。如果要使用 INT 值,则需要在调用中指定它
$stmt->bindParam(':category_id', $this->category_id, PDO::PARAM_INT);
推荐阅读
- php - 从mysql存储过程获取输出值到php?
- assembly - 指令表中缺少延迟
- excel - 运行时错误 1004 无法使用 url 链接名称查找列
- python-3.x - 在 Windows 上“导入 tensorflow”后 python 出错
- java - GC Log 中的应用时间长
- javascript - 每 n 次迭代 adsense 代码:错误意外 EOF
- java - 如何打印两个字符并在它们之间留一个空格?
- java - 从 weblogic 运行时 XML 响应会发生变化吗?
- python - 如何让 Python 包不需要 import package.package
- ios - 如何快速解决日历问题?(很高兴即使在objective-c中也能看到)