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 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式