dsh102123 2018-07-05 08:34
浏览 112
已采纳

未捕获的PDOException:SQLSTATE [HY093]:参数号无效:参数未定义[重复]

This question already has an answer here:

Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined on line 73.

What is this error ?

$stmtExec = $stmt->execute(); // the line 73

Employee.php

public function update($fields,$id){
    $st = "";
    $counter = 1;
    $total_fields = count($fields);
    foreach ($fields as $key => $value) {
        if ($counter === $total_fields) {
            $set = "$key = :".$key;
            $st = $st.$set;
        } else{
            $set = "$key = :".$key.",";
            $st = $st.$set;
            $counter++;
        }
    }

    $sql = "";
    $sql.= "UPDATE info SET".$st;
    $sql.= "WHERE id =".$id;

    $stmt = $this->connect()->prepare($sql);

    foreach ($fields as $key => $value) {
        $stmt->bindValue(':'.$key, $value);
    }

    $stmtExec = $stmt->execute(); // here is the line with the error

    if ($stmtExec) {
        header('Location:index.php');
    }
}
</div>
  • 写回答

1条回答 默认 最新

  • dqed19166 2018-07-05 08:41
    关注

    There are some syntax errors. Like no space after SET and before WHERE keyword. I have added and updated your code.

    public function update($fields, $id) {
        $st = "";
        $counter = 1;
        $total_fields = count($fields);
        foreach ($fields as $key => $value) {
            if ($counter === $total_fields) {
                $st .= "$key = :" . $key;
            } else {
                $st .= "$key = :" . $key . ",";
                $counter++;
            }
        }
        $sql = "UPDATE info SET " . $st . " WHERE id =" . $id;
    
        $stmt = $this->connect()->prepare($sql);
    
        foreach ($fields as $key => $value) {
            $stmt->bindValue(':' . $key, $value);
        }
    
        $stmtExec = $stmt->execute();
    
        if ($stmtExec) {
            header('Location:index.php');
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog