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

致命错误:未捕获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 快手联盟怎么快速的跑出建立模型
  • ¥20 ros环境下运行orb-slam一直报错
  • ¥15 关于腾讯云服务器中,PHP页面无法打开的问题
  • ¥60 如何把照片修复成原始拍摄图
  • ¥80 Exited too quickly (process log may have details)
  • ¥15 爬知乎登录之后内容加载不出来
  • ¥15 怎么用protues测量通频带
  • ¥15 zepelin使用sparkInterpreter 异常
  • ¥15 paho mqtt 接收不到消息
  • ¥15 函数r关于两个分量y,z方向上的图像,分开画r随y的图像,r随z的图像