繁体   English   中英

php尝试制作OOP登录系统时出错

[英]php Error trying to make a OOP login system

尝试在系统中进行面向对象的登录时,我一直收到警告错误。 我现在正在尝试执行插入功能,但警告阻止了我。

警告:第32行的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不是数组,因此不是可遍历的。 像这样在foreach之前先做个小检查。

    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无法正常工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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