尝试建立面向对象登录系统时出错


php Error trying to make a OOP login system

在尝试使这个面向对象的登录系统时,我一直得到警告错误。我正在尝试执行插入函数,但警告阻止了我。

警告:为C:'xampp'htdocs'YetiDraft'yetidb'classes'db.php中的foreach()提供的参数无效

第32行是函数查询foreach语句

<?php
class db {
    private static $_instance = null;
    private $_pdo,
            $_query, 
            $_error = false, 
            $_results, 
            $_count = 0;
    private function __construct() {
        try {
            $this->_pdo = new PDO('mysql:host=' . config::get('mysql/host') . ';dbname=' . config::get('mysql/db'), config::get('mysql/username'), config::get('mysql/password'));
        } catch(PDOException $e) {
            die($e->getMessage());
        }
    }
    public static function getInstance(){
        if(!isset(self::$_instance)){
            self::$_instance = new db();
        }
        return self::$_instance;
    }
    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++;
                }   
            }
            if($this->_query->execute()){
                $this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
                $this->_count = $this->_query->rowCount();
            }else{
                $this->_error = true;
            }
        }
        return $this;
    }

    public function action($action, $table, $where = array()){
        if(count($where) === 3){
            $operators = array('=', '>', '<', '>=', '<=',);
            $field = $where[0];
            $operator = $where[1];
            $value = $where[2];
            if(in_array($operator, $operators)){
                $sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
                if(!$this->query($sql, array($value))->error()){
                    return $this;
                }
            }
        }
        return false;
    }
    public function get($table, $where){
        return $this->action('SELECT *', $table, $where);
    }
    public function delete($table, $where){
        return $this->action('DELETE', $table, $where);
    }
    public function insert($table, $fields = array()){
        if(count($fields)){
            $keys = array_keys($fields);
            $values = '';
            $x = 1;
            foreach($fields as $fields){
                $values .= "?";
                if($x < count($fields)){
                    $values .= ', ';
                }
                $x++;
            }
            $sql = "INSERT INTO users (`" . implode('`, `', $keys) ."`) VALUES ({$values})";
            if(!$this->query($sql, $fields)->error()){
                return true;
            }
        }
        return false;
    }
    public function results(){
        return $this->_results;
    }
    public function first(){
        return $this->results()[0];
    }
    public function error(){
        return $this->_error;
    }
    public function count() {
        return $this->_count;
    }
}

?>

这部分可能会导致问题:

foreach($fields as $fields){
    $values .= "?";
    if($x < count($fields)){
         $values .= ', ';
    }
    $x++;
}

您使用变量$fields作为源和迭代器。

这样做会用数组的最后一个元素覆盖$fields。因此,在下面的调用中,$fields不再是一个数组:

if(!$this->query($sql, $fields)->error()){

由于您试图在$this->query()中迭代$fields的值,因此您会得到错误。

感谢Barmar的提示!


试试这个:

foreach($fields as $field){
    $values .= "?";
    if($x < count($fields)){
         $values .= ', ';
    }
    $x++;
}

编辑:或者使用Barmar的解决方案。

我猜$params不是一个数组,因此不能遍历。

    if (is_array($params))
    {
    foreach ($params as $param)
    {
    //do things here
    }
    }

count()不是检查变量是否为数组的可靠方法。使用is_array代替count()。您的错误可能是变量不是数组,而您试图遍历该变量

替换循环:

foreach($fields as $field){
    $values .= "?";
    if($x < count($field)){
         $values .= ', ';
    }
    $x++;
}

$values = implode(', ', array_fill(0, count($fields), '?'));

因为您重用变量$fields作为迭代变量,当您的循环完成时,$fields不再包含字段数组,它包含最后一个字段的值。所以当你调用$this->query($sql, $fields)时,你传递的是一个字符串而不是一个数组,query()中的foreach将不起作用。