将变量的查询值设置为null

时间:2013-04-19 21:47:36

标签: php mysql sql

我正在尝试使用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,但是以下都不会将parent设置为null,而是将值设置为空字符串“”或空字符串“NULL”。

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

可以采取哪些措施来解决这个问题?

1 个答案:

答案 0 :(得分: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);