PDO和bindParam问题

时间:2011-12-16 22:49:45

标签: php

好的,这是我的代码......

public function insert_cover($title, $description, $category_id, $filename)
{
    if($this->category_exists($category_id)) {
        $sth = $this->con->prepare('
            INSERT INTO cover
            (id, title, description, image, category_id, timestamp)
            VALUES
            (null, :title, :description, :image, :category_id, null)
        ');

        $sth->bindParam(':title', $title, PDO::PARAM_STR);
        $sth->bindParam(':description', $description, PDO::PARAM_STR);
        $sth->bindParam(':image', $filename);
        $sth->bindParam('::category_id',  $category_id, PDO::PARAM_INT);

        $sth->execute();

        return true;
    } else {
        return false;
    }
}

这是我的错误:

Warning: PDOStatement::execute() [pdostatement.execute]: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined in /MAMP/htdocs/*****/lib/DB.class.php on line 44

如果我不使用bindParam并添加key =>数组,它可以正常工作值对execute()

有什么想法在这里发生了什么?

2 个答案:

答案 0 :(得分:3)

可能是$sth->bindParam('::category_id', $category_id, PDO::PARAM_INT);中的拼写错误 正确:$sth->bindParam(':category_id', $category_id, PDO::PARAM_INT);

答案 1 :(得分:0)

category_id有两个冒号。