简体   繁体   中英

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

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.\n", $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

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.\n", $stmt->error);
      return false;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM