chengkun
2025-04-29 a34a90f03cecc7658231547270f8036bb5affe4a
app/public/action.class.php
@@ -1,7 +1,6 @@
<?php
class model {
    // 操作状态
    const MODEL_INSERT = 1;      //  插入模型数据
@@ -152,33 +151,41 @@
    protected function _initialize() {
    }
    
    function get_table_fields($tableName) {
        include(CONFIG_PATH . 'db.config.php');
        $mysqli = new mysqli($db_config['dbhost'], $db_config['dbuser'], $db_config['dbpass'], $db_config['dbname']);
        $query = $mysqli->query("SHOW COLUMNS FROM $tableName");
        $tableFields = array();
        while ($row = $query->fetch_assoc()) {
            $tableFields[] = $row['Field'];
        }
        return $tableFields;
    }
    
    function insert_into($table, $data = array()) {
        $value = array();
        $this->db->connect();
        include(PLUS_PATH . 'dbstruct.cache.php');
        $TableFullName = $this->def . $table;
        if (is_array($$TableFullName)) {
            $fields = array_keys($$TableFullName);
        } else {
        $fields = $this->get_table_fields($TableFullName);
        if (!$fields) {
            return FALSE;
        }
        if (is_array($fields)) {
            if (is_array($data)) {
                foreach ($data as $key => $v) {
                    if (in_array($key, $fields)) {
                        $v = $this->FilterStr($v);
                        $value[] = "`" . $key . "`='" . $this->db->escape_string($v) . "'";
                    }
                }
        if (!is_array($data)) {
            return FALSE;
        }
        foreach ($data as $key => $v) {
            if (in_array($key, $fields)) {
                $v = $this->FilterStr($v);
                $value[] = "`" . $key . "`='" . $this->db->escape_string($v) . "'";
            }
        }
        $value = @implode(",", $value);
        if (!$value) {
            return FALSE;
        }
        return $this->DB_insert_once($table, $value);
    }
    
@@ -187,8 +194,16 @@
        $this->db->connect();
        $value = array();
        include(PLUS_PATH . 'dbstruct.cache.php');
//        $TableFullName=$this->def.$table;
//        var_dump($$TableFullName);exit();
        $TableFullName = $this->def . $table;
        $fields = $this->get_table_fields($TableFullName);
        if (!is_array($data) || !is_array($where)) {
            return FALSE;
        }
        if (!$fields) {
            return FALSE;
        }
        //        var_dump($$TableFullName);exit();
//        if (is_array($where)) {
//            $fields = array_keys($where);
//        } else {
@@ -197,57 +212,56 @@
//        var_dump($fields);exit();
        
        
        if (is_array($data)) {
            foreach ($data as $key => $v) {
        foreach ($data as $key => $v) {
            if (!in_array($key, $fields)) {
                continue;
            }
            if (is_array($v)) {
                
                if (is_array($v)) {
                if ($v[0] == '+') {
                    
                    if ($v[0] == '+') {
                    $value[] = '`' . $key . '` = `' . $key . '` + ' . $this->db->escape_string($v[1]);
                } elseif ($v[0] == '-') {
                    $value[] = '`' . $key . '` = `' . $key . '` - ' . $this->db->escape_string($v[1]);
                } else if ($v[0] == '=') {
                    $value[] = '`' . $key . '`  = ' . $this->db->escape_string($v[1]);
                } elseif ($v[0] == 'CASE') {
                    $casesql = '`' . $key . '` =  CASE `' . $this->db->escape_string($v[1]) . '`';
                    foreach ($v[2] as $ck => $cv) {
                        
                        $value[] = '`' . $key . '` = `' . $key . '` + ' . $this->db->escape_string($v[1]);
                    } elseif ($v[0] == '-') {
                        $value[] = '`' . $key . '` = `' . $key . '` - ' . $this->db->escape_string($v[1]);
                    } else if ($v[0] == '=') {
                        $value[] = '`' . $key . '`  = ' . $this->db->escape_string($v[1]);
                    } elseif ($v[0] == 'CASE') {
                        $casesql = '`' . $key . '` =  CASE `' . $this->db->escape_string($v[1]) . '`';
                        foreach ($v[2] as $ck => $cv) {
                            $casesql .= " WHEN '" . $ck . "' THEN '" . $this->db->escape_string($cv) . "' ";
                        }
                        $casesql .= 'END';
                        $value[] = $casesql;
                    } elseif ($v[0] == 'DATE_ADD') {
                        $value[] = '`' . $key . '` = DATE_ADD(`' . $key . '` , INTERVAL ' . $this->db->escape_string($v[1]) . ' DAY )';
                    } elseif ($v[0] == 'concat') {
                        $value[] = '`' . $key . '` = concat(`' . $key . '` , ",' . $this->db->escape_string($v[1]) . ' ")';
                    } else {
                        $this->db->show_error();
                        $casesql .= " WHEN '" . $ck . "' THEN '" . $this->db->escape_string($cv) . "' ";
                    }
                    
                    $casesql .= 'END';
                    $value[] = $casesql;
                } elseif ($v[0] == 'DATE_ADD') {
                    $value[] = '`' . $key . '` = DATE_ADD(`' . $key . '` , INTERVAL ' . $this->db->escape_string($v[1]) . ' DAY )';
                } elseif ($v[0] == 'concat') {
                    $value[] = '`' . $key . '` = concat(`' . $key . '` , ",' . $this->db->escape_string($v[1]) . ' ")';
                } else {
                    
                    $v = $this->FilterStr($v);
                    $value[] = "`" . $key . "`='" . $this->db->escape_string($v) . "'";
                    $this->db->show_error();
                }
                
            } else {
                $v = $this->FilterStr($v);
                $value[] = "`" . $key . "`='" . $this->db->escape_string($v) . "'";
            }
        }
        
        
@@ -543,6 +557,13 @@
        }
        include(PLUS_PATH . 'dbstruct.cache.php');
        $TableFullName = $this->def . $tablename;
        /////获取表的字段
        if (!isset($$TableFullName)) {
            $this->error = '表结构缓存文件不存在';
            return FALSE;
        }
        if (is_array($$TableFullName)) {
            $fieldsArr = array_keys($$TableFullName);
        }