Php 更新数据库类中的delete函数,以排除两组条件

Php 更新数据库类中的delete函数,以排除两组条件,php,mysql,pdo,Php,Mysql,Pdo,我目前有一个数据库类,它有许多处理一些常见数据库操作的函数,可以在整个项目中重用。我希望通过引入两组字段和值,使delete函数更加健壮,并在这个过程中使用户更难猜测要删除的有效ID 数据库类中的相关函数 class DB{ private static $_instance = null; private $_pdo, $_query, $_error = false, $_results,

我目前有一个数据库类,它有许多处理一些常见数据库操作的函数,可以在整个项目中重用。我希望通过引入两组字段和值,使delete函数更加健壮,并在这个过程中使用户更难猜测要删除的有效ID

数据库类中的相关函数

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 delete($table, $where){
        return $this->action('DELETE', $table, $where);
    }
所需用途

try{
    $messageDelete = DB::getInstance()->delete('message_table', array('message_id', '=', $message_id));
}catch(Exception $e){
    die($e->getMessage());
}
try{
    $messageDelete = DB::getInstance()->delete('message_table', array('message_id', '=', $message_id, 'AND', 'user_id', '=', $user_id));
}catch(Exception $e){
    die($e->getMessage());
}
如果您有任何想法,我们将不胜感激:)

尝试以下变体:

public function action($action, $table, $wheres = array(), $whereOperator = "AND")
{
    $conditions = "";
    $queryParams = array();

    foreach($wheres as $where){
        if(count($where) === 3){
            $operators = array('=', '>', '<', '>=', '<=');

            $field      = $where[0];
            $operator   = $where[1];
            $value      = $where[2];
            if($where === end($wheres)) $whereOperator = '';                

            if(in_array($operator, $operators)){                    
                $conditions = $conditions." {$field} {$operator} ? ".$whereOperator;
                array_push($queryParams, $value);
            }
        }
    }

    $sql = "{$action} FROM {$table} WHERE ".$conditions;
    var_dump($sql, $queryParams); die();
    if(!$this->query($sql, $queryParams)->error()){
        return $this;
    }

    return false;
}

我对这种方法做了一些修改(我没有将其包括在示例中,但action()在类的其他地方使用),它起到了很好的效果!谢谢您。
try{
    $messageDelete = DB::getInstance()->delete('message_table', array(array('message_id', '=', $message_id), array('user_id', '=', $user_id)), "AND");
}catch(Exception $e){
    die($e->getMessage());
}