gpt4 book ai didi

php - 更新数据库类中的删除函数,以排除两组条件

转载 作者:行者123 更新时间:2023-11-29 10:23:44 25 4
gpt4 key购买 nike

我目前有一个数据库类,它有许多处理一些常见数据库操作的函数,可以在整个项目中重用。我希望通过接受两组字段和值来使删除功能更加强大,并在此过程中使用户更难猜测要删除的有效 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());
}

任何想法将不胜感激:)

最佳答案

尝试这个变体:

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

关于php - 更新数据库类中的删除函数,以排除两组条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48730574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com