更新数据库时PHP mysql错误

时间:2014-03-13 10:34:15

标签: php mysql sql syntax pdo

我收到以下错误

Warning: PDOStatement::execute(): SQLSTATE[HY093]: 
Invalid parameter number: number of bound variables does not match number of tokens

当我试图更新MySQL数据库时。

这是我使用的代码。

public function update() {
    global $db;
    $stmt = $db->prepare("UPDATE products SET Name='?', Cate_id='?', Price='?', Image='?', Special='?', Special_price='?', Disable='?' WHERE PID = ?;");
    $stmt->execute(array($this->name, $this->category, $this->price, $this->image, $this->special, $this->special_price, $this->disable, $this->id));
}

2 个答案:

答案 0 :(得分:3)

SET Name='?', Cate_id='?', Price='?', Image='?'  etc

不需要引用?

应该是

SET Name=?, Cate_id=?, Price=?, Image=? etc...

答案 1 :(得分:0)

请遵循以下内容:

$stmt = $db->prepare("UPDATE products SET Name='?', Cate_id=?, Price=?, Image=?, Special=?, Special_price=?, Disable=? WHERE PID = ?;");

$stmt->bind_param($this->name, $this->category, $this->price, $this->image, $this->special, $this->special_price, $this->disable, $this->id);

$stmt->execute();