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

致命错误:未捕获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

图片转代码服务由CSDN问答提供 功能建议

我正在尝试通过php运行更新查询到postgresql数据库。 当我尝试这样做时,我得到一个错误。

我尝试将 id =?更改为 id =:id 但是它 不起作用

我的 update 函数:

  //更新学生
公共函数updateStudent  (){
 $ query ='UPDATE'。  $ this->表。  '(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; 
} 
 //打印错误
 printf(“错误:%s。
”  ,$ stmt->错误); 
返回false; 
} 
   
 
 

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

它表示错误在第58行,即行: $ stmt = $ this-> conn-> prepare($ query); 我的错误在58以上的行内。

更新: 如果我 使用 id =:id 而不是 id = ?,我收到以下错误:

致命错误:未捕获PDOException:SQLSTATE [42601]:语法错误:7错误:语法错误在或附近“( “第1行:更新学生(姓名,课程)价值($ 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;
        }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题