PHP PDO /事务更新Statmement产生错误(SQLSTATE [HY093]:参数号无效:参数未定义)[重复]

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (56)

这个问题在这里已有答案:

在以下函数中,我有两个更新语句。第一个执行正常但第二个执行不正确,产生错误(无效的参数编号)。阈值是浮点数(例如,0.55,最多两位数),而可能性和结果是整数)。

如何更正我的代码以避免产生此错误?

public function updateAll($params = [])
{
    $sql = "update
            risklevels
            set
                riskmaximum = :riskmaximum,
                riskhigh = :riskhigh,
                riskmedium = :riskmedium,
                riskminimum = :riskminimum
            where risklevelid = 1";

    $sql2 = "update
             riskmatrixthresholds
             set 
                level = :level
             where likelihood = :likelhood
             and consequence = :consequence";
    try
    {    
        $this->db->beginTransaction();  
        $statement = $this->db->prepare($sql);
        $statement->bindValue(':riskmaximum', $params['Levels']['riskmaximum']);
        $statement->bindValue(':riskhigh' , $params['Levels']['riskhigh']);
        $statement->bindValue(':riskmedium' , $params['Levels']['riskmedium']);
        $statement->bindValue(':riskminimum' , $params['Levels']['riskminimum']);

        $statement->execute(); 

        for ($l = 1; $l <= 5; $l++)
        {
            for($c = 1; $c <= 5; $c++)
            {
                $threshold = $params['Thresholds'][$l][$c];
                $statement2 = $this->db->prepare($sql2);  
                $statement2->bindValue(':level', $threshold);
                $statement2->bindValue(':likelihood', $l);
                $statement2->bindValue(':consequence', $c);
                $statement2->execute();
            }
        }
        $this->db->commit();

        return ["Succeeded" => true, "Result" => "Risk Configuration Updated!"];
    }
    catch (\PDOException $e)
    {
        return ["Succeeded" => false, "Result" => $e->getMessage()];
    }
}
提问于
用户回答回答于

你拼错了一个SQL变量。

更改where likelihood = :likelhoodwhere likelihood = :likelihood

扫码关注云+社区

领取腾讯云代金券