绑定PDO的参数数目无效


Invalid number of parameters binding PDO

我正在为这个项目使用教程,但是我正在尝试扩展材料中提供的内容。我正在尝试使用一个函数,该函数在绑定值之前创建查询,以创建一个具有超过3个WHERE值的查询。

代码如下:

private function action($action, $table, $where = array()){
    $operators = array('=', '>', '<', '>=', '<=' , 'AND' ,'OR', 'LIKE', 'GROUP BY','ORDER BY', 'ASC', 'DESC');
if(!empty($where)){
    $sql = "{$action} FROM {$table} WHERE ";
    if(count($where) > 3){
            $isVal = FALSE;
            $values = '';
            foreach ($where as $value) {
                    switch(trim($value)){
                        case '=':
                        case '>':
                        case '<':
                        case '>=':
                        case '<=':
                            $sql .= "{$value}";
                            $isVal = true;
                        break;
                        default:
                            if($isVal){
                                $sql .= " ? ";
                                $values .= $value;
                                $isVal = false;
                            }else{
                                $sql .= "{$value}";
                            }
                        break;
                    }
            }
        if(!$this->query($sql, $values)->error()){return $this;}
  /////////////////////////////////////////
 // From this point down everything works!!!
 ////////////////////////////////////////////
    }else if(count($where) === 3){
        $field      = $where[0]; 
        $operator   = $where[1];
        $value      = $where[2];
        if(in_array($operator, $operators)){
            $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?"; // NO $value ?
            if(!$this->query($sql, array($value))->error()){return $this;}
        }
    }
}else{
    // If array is empty
    $sql = "{$action} FROM {$table}";
    if(!$this->query($sql)->error()){return $this;}
}
return FALSE;
}

嵌套Else IF语句读取count($where) === 3工作正常,但是第一个嵌套IF 'count($where> 3) '抛出错误。

我正试图找到一种方法来正确设置,这样我就可以使用超过3个where值。

这是我的查询绑定:

public function query($sql, $params = array()){
        $this->_error = FALSE;
        if($this->_query = $this->_pdo->prepare($sql)){
            $x = 1;
            if(count($params)){
                foreach($params as $param){
                    $this->_query->bindValue($x, $param);
                    $x++;
                }
            }// End IF count    
            if($this->_query->execute()){
                    $this->_lastID = $this->_pdo->lastInsertId();
                    try{
                        $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                    }catch(Exception $e){
                        // Catch Error
                    }
                    $this->_count = $this->_query->rowCount();
            }else{
                $this->_error = TRUE;}
        }
        return $this;
    }
如果有人能帮我解决这个问题,我将非常感激……谢谢你!

这里有一些问题,例如:

    第一个方法中的
  1. $values是一个连接的字符串。将其转换为数组将给你一个只有一个元素的数组,这不是你需要的。你需要一个数组,所有的值分开,像$values[] = $value;代替$values .= $value;
  2. 当你尝试添加ORAND语句的组合时,你会遇到问题,因为使用括号分组会有很大的不同。
  3. 您正在为值使用准备好的语句,但没有对列名进行检查以防止sql注入。你应该使用白名单。