如何更新pdo中的记录

How to update records in pdo

更新:这不是 "Invalid parameter number: parameter was not defined" Inserting data 这个问题是针对 Yii 框架的,提问者用错了变量,他说这个问题应该可以去掉,因为这个问题一般不在 PDO 上。这是一个不同的问题。

我的 CMS 的此代码旨在更新每个产品已经存在的产品信息。目前,当我更新信息时,我 return 到同一页面并收到此错误,并且针对每个尝试更新的产品回显了信息...

Total: 5, Priority: 1, Label: Clothing, Value: Shirt, Visible: true, ID:34

"UPDATE product_attributes SET priority = :priority, label = :label, value = :value, single_line = :single_line WHERE product_id = :product_id "

SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

$id = $_GET['id'];
for($i = 0; $i < $totalProducts; $i++){
    if(!empty($_POST)){
        try {
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "UPDATE product_attributes SET priority = :priority,
                                                                label = :label,
                                                                value = :value,
                                                                visible = :visible 
                                                     WHERE product_id = :product_id";
            //Prepare Statement
            $query = $pdo->prepare($sql);
            $query->bindValue(' :priority ', $_POST['priority' . $i]);
            $query->bindValue(' :label ', $_POST['label' . $i]);
            $query->bindValue(' :value ', $_POST['value' . $i]);
            $query->bindValue(' :visible ', $_POST['visible'  . $i]);
            $query->bindValue(' :product_id ', $id);

            //Execute the query
            $query->execute();  

            // echo a message to say the UPDATE succeeded
            echo $query->rowCount() . " records UPDATED successfully";
        }
        catch(PDOException $e)
        {
            echo "<br>Priority:" . $_POST['priority' . $i] . "<br>";//currently echoes "1"
            echo "<br>Label:" . $_POST['label' . $i] . "<br>";//currently echoes "clothing"
            echo "<br>Value:" . $_POST['value' . $i] . "<br>";//currently echoes "shirt"
            echo "<br>Visible:" . $_POST['visible' . $i] . "//<br>";//echoes "true"
            echo "<br>ID:" . $id . "<br>";//currently echoes "34"

            echo '"' . $sql . " \"<br>" . $e->getMessage() . "<br>";
        }
    }
}

但是,我有等量的绑定值和正在更新的 SQL 字段。我一直在搜索其他已回答的问题,但其中 none 解决了我的问题。为什么说没有定义参数?

字符串

' :priority '

参数名称周围有两个额外的空格。