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 ssh登录页面的问题
  • ¥60 渗透一个指定银行app,拿到客户信息,需要什么级别
  • ¥50 关于在matlab上对曲柄摇杆机构上一点的运动学仿真
  • ¥15 jetson nano
  • ¥15 :app:debugCompileClasspath'.
  • ¥15 windows c++内嵌qt出现数据转换问题。
  • ¥15 stm32 串口通讯过程中的问题
  • ¥20 公众号如何实现点击超链接后自动发送文字
  • ¥15 用php隐藏类名和增加类名
  • ¥15 算法设计与分析课程的提问