ecmsapi icon indicating copy to clipboard operation
ecmsapi copied to clipboard

EapiDb insert bug

Open pndx opened this issue 3 years ago • 0 comments

public function insert($table , $data = [])
    {
        if(empty($table) || empty($data) || !is_array($data)){
            return false;
        }
        $field = "";
        $value = "";
        foreach($data as $f=>$v){
            $field .= "," . $f;
            $value .= ",'" . $v ."'";//当写入值包含单引号会导致SQL错误,无法写入
        }
        $field = substr($field , 1);
        $value = substr($value , 1);
        $sql = "insert into {$table} ({$field}) values ({$value});";
        $res = $this->query($sql , false);
        if(true === $res){
            return $this->empire->lastid();
        }else{
            return false;
        }
    }

以下是我的修复

public function insert($table , $data = [])
    {
        if(empty($table) || empty($data) || !is_array($data)){
            return false;
        }
        $field = "";
        $value = "";
        foreach($data as $f=>$v){
            $field .= "," . $f;
            if (strpos($v, "'") !== false) {
                $v = str_replace("'", "''", $v);
            }
            $value .= ",'" . $v ."'";
        }
        $field = substr($field , 1);
        $value = substr($value , 1);
        $sql = "insert into {$table} ({$field}) values ({$value});";
        $res = $this->query($sql , false);
        if(true === $res){
            return $this->empire->lastid();
        }else{
            return false;
        }
    }

参考链接

pndx avatar Sep 07 '21 07:09 pndx