更新数据库类中的删除功能,除了两组条件

时间:2018-02-11 10:34:56

标签: php mysql pdo

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

在DB类中重新发布函数

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

任何想法都将受到赞赏:)

1 个答案:

答案 0 :(得分:1)

尝试此变体:

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

这适用于方法调用:

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