dounong5373 2019-07-31 16:30
浏览 174
已采纳

致命错误:未捕获PDOException:SQLSTATE [HY093]:参数号无效:混合命名和位置参数

I am trying to run an update query through php to a postgresql database. When i try to do that i get an error.

I tried changing the id = ? to id = :id but it didn't work

My update function:

//update a student
    public function updateStudent(){
      $query = 'UPDATE ' . $this->table . ' ( name, course) VALUES ( :name, :course) WHERE id = ? ;';
      $stmt = $this->conn->prepare($query);

      $this->id = htmlspecialchars(strip_tags($this->id));
      $this->name = htmlspecialchars(strip_tags($this->name));
      $this->course = htmlspecialchars(strip_tags($this->course));
      $stmt->bindParam(':id', $this->id);
      $stmt->bindParam(':name', $this->name);
      $stmt->bindParam(':course', $this->course);

      if($stmt->execute()){
        return true;
      }
      //print error
      printf("Error: %s.
", $stmt->error);
      return false;
    }

The error: Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: mixed named and positional parameters in...

It says the error is on line 58 which is the line that reads: $stmt = $this->conn->prepare($query); I the error is within the line above 58.

Update: If i use id = :id instead of id = ?, i get the following error:

Fatal error: Uncaught PDOException: SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "(" LINE 1: UPDATE students ( name, course) VALUES ( $1, $2) WHERE id = ... ^ in

  • 写回答

1条回答 默认 最新

  • dsc80135 2019-07-31 16:48
    关注

    you can't use :param and ? use :id instead of ?

    but for update you could use

        'UPDATE ' . $this->table . ' 
            set  name = :name, 
             course = :course 
          WHERE id = :id ;';
    
    
    
    
       public function updateStudent(){
          $query = 'UPDATE ' . $this->table . ' 
                      set  name = :name, 
                           course = :course 
                    WHERE id = :id ;';
          $stmt = $this->conn->prepare($query);
    
          $this->id = htmlspecialchars(strip_tags($this->id));
          $this->name = htmlspecialchars(strip_tags($this->name));
          $this->course = htmlspecialchars(strip_tags($this->course));
          $stmt->bindParam(':id', $this->id);
          $stmt->bindParam(':name', $this->name);
          $stmt->bindParam(':course', $this->course);
    
          if($stmt->execute()){
            return true;
          }
          //print error
          printf("Error: %s.
    ", $stmt->error);
          return false;
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵