PDO:我的User-> logout()中的DELETE语句未执行

问题描述 投票:-2回答:1

我的错误:

致命错误:未捕获的PDOException:SQLSTATE [42000]:语法错误或访问冲突:1064您的SQL语法有错误;请参阅附录A。检查与您的MariaDB服务器版本相对应的手册,以在'* FROM tbl_usersession WHERE BenutzerID =?附近使用正确的语法。在C:\ xampp7-4-3 \ htdocs \ Corona \ classes \ db.php:52的第1行上:堆栈跟踪:#0 C:\ xampp7-4-3 \ htdocs \ Corona \ classes \ db.php(52) :PDO-> prepare('DELETE * FROM t ...')#1 C:\ xampp7-4-3 \ htdocs \ Corona \ classes \ db.php(94):DB-> query('DELETE * FROM t ... ...',数组)#2 C:\ xampp7-4-3 \ htdocs \ Corona \ classes \ db.php(111):DB-> action('DELETE *','tbl_usersession',Array)#3 C :\ xampp7-4-3 \ htdocs \ Corona \ classes \ user.php(135):DB-> delete('tbl_usersession',Array)#4 C:\ xampp7-4-3 \ htdocs \ Corona \ logout.php (5):User-> logout()#5 {main}在第52行的C:\ xampp7-4-3 \ htdocs \ Corona \ classes \ db.php中抛出

代码:

<?php
require_once 'core/init.php';

$user = new User();
$user->logout();

// Redirect::to('index.php');



?>

我的用户类别:

<?php
class User
{
    private $_db,
            $_data,
            $_sessionName,
            $_cookieName,
            $_isLoggedIn;

    public function __construct($user = null)
    {
        $this->_db = DB::getInstance();
        $this->_sessionName = Config::get('session/session_name');
        $this->_cookieName = Config::get('remember/cookie_name');

        if(!$user)
        {
            if(Session::exists($this->_sessionName))
            {
                $user = Session::get($this->_sessionName);

                if($this->find($user))
                {
                    $this->_isLoggedIn = true;
                }
                else
                {
                    //process logout
                }
            }
        }
        else
        {
            $this->find($user);
        }
    }

    public function create($fields = array())
    {
        if
        (
            $this->_db->insert('tbl_benutzer', $fields)
        )
        {
            throw new Exception('Es gab einen Fehler bei der Erstellung Ihres Kontos.');
        }
        echo "Ihr Benutzerkonto wurde erfolgreich angelegt. Sie können sich jetzt anmelden.";
    }

    public function find($email = null)
    {
        if($email)
        {
            $field = (is_numeric($email)) ? 'id' : 'Email';
            $data = $this->_db->get('tbl_benutzer', array($field, '=', $email));

            if($data->count())
            {
                $this->_data = $data->first();
                return true;

            }
            return false;
        }


    }

    public function login($email = null, $password = null, $remember = false)
    {
        // echo "Remember=" . $remember . "<br>";
        $user = $this->find($email);

        if(!$email && !$password && $this->exists())
        {
            Session::put($this->_sessionName, $this->data()->ID);
        }
        else
        {
            $user = $this->find($email);
            if($user)
            {
                if(password_verify($password, $this->data()->Hash))
                {
                    Session::put($this->_sessionName, $this->data()->ID);
                    echo "Remember=" . $remember . "<br>";

                    if($remember)
                    {
                        $hash = Hash::unique();
                        echo "Hash=" . $hash . "<br>";
                        echo "id=" . $this->data()->ID . "<br>";
                        $hashCheck = $this->_db->get('tbl_usersession', array('BenutzerID', "=", $this->data()->ID));
                        echo "HashCheckCount= " . $hashCheck->count() . "<br>";

                        if(!$hashCheck->count())
                        {
                            $this->_db->insert
                            (
                                'tbl_usersession',
                                array
                                (
                                'BenutzerID' => $this->data()->ID,
                                'Hash' => $hash
                                )
                            );
                        }
                        else
                        {
                            $hash = $hashCheck->first()->Hash;
                        }
                    }

                    Cookie::put($this->_cookieName, $hash, Config::get('remember/cookie_expiry'));

                    return true;
                }
                else return false;
            }
        }



        return false;

    }

    public function exists()
    {
        return (!empty($this->data)) ? true : false;
    }

    public function logout()
    {
        $this->_db->delete('tbl_usersession', array('BenutzerID', '=', $this->data()->ID));
        print_r($this->data());
        // Wieso geht das delete nicht?
        Session::delete($this->_sessionName);
        Cookie::delete($this->_cookieName);
    }

    public function data()
    {
        return $this->_data;
    }


    public function isLoggedIn()
    {
        return $this->_isLoggedIn; 
    }
}



?>

我的数据库类:

<?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')
            );

            // Error tracking:
            $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $this->_pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        }

        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)))
                {
                    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 = null;
            $x = 1;

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


            $sql = "INSERT INTO " . $table . " (" . implode(", ", $keys) . ") VALUES ({$values})";

            if
            (
                $this->query($sql, $fields)->error()
            )
            {
                return true;
            }
        }
    }

    public function update($table, $id, $fields = array())
    {
        $set = ' ';
        $x = 1;

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

        }

        $sql = "UPDATE {$table} SET {$set} WHERE ID = {$id}";

        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;
    }
}



?>
pdo sql-delete
1个回答
0
投票

这只是DELETE语句的基本语法错误,如下所示:

正确的语法是

DELETE FROM table 

但是您错误的语法是

DELETE * FROM table

因此调试您的delete函数将解决您的语法错误

public function delete($table, $where)
{
    return $this->action('DELETE', $table, $where);   
}
© www.soinside.com 2019 - 2024. All rights reserved.