0

我正在尝试使用 PDO 将空值插入数据库,但似乎有问题

代码

public function addBookCategory($id, $isbn, $category, $parent){
    $id = $this->dbh->quote($id);
    $isbn = $this->dbh->quote($isbn);
    $category = $this->dbh->quote($category);
    $parent = $this->dbh->quote($parent);

    return $this->query(
        "INSERT INTO book_categories 
        VALUES($id, $isbn, $category, $parent) 
        ON DUPLICATE KEY UPDATE id = $id"
    );
}

在某些情况下,父列可以为空,但以下均未将父列设置为空,而是将值设置为空字符串“”或空字符串“NULL”。

$database->addBookCategory("kujhg", "asdasd", "asdasd", NULL);
$database->addBookCategory("kujhg", "asdasd", "asdasd", "");
$database->addBookCategory("kujhg", "asdasd", "asdasd", "NULL");

可以做些什么来克服这个问题?

4

1 回答 1

0

你应该写列名

   "INSERT INTO book_categories (id, isbn , category,parent)
    VALUES($id, $isbn, $category, $parent) 
    ON DUPLICATE KEY UPDATE id = $id"

然后用这个

  $database->addBookCategory("kujhg", "asdasd", "asdasd", NULL);
于 2013-04-19T21:51:39.003 回答