更新表单提交编码器上的数据库


updating database on form submit codeigniter

我在表单提交的数据库中更新我的值时遇到了麻烦。我使用codeigniter 2.20

我得到一个错误致命错误:调用未定义的方法Model_setting::updateTheme()在E:'Xampp'htdocs'codeigniter-theme'admin'controllers'setting'setting.php在第8行

我想存档的是一旦选择了主题的形式,它将更新设置表的值是它被张贴到的地方。表单提交时也不会改变。我有自动加载的form_validation库和form helper.

<?php
class Model_setting extends CI_Model {
    public function updateTheme() {
        $this->db->select('*');
        $this->db->where('group', 'config');
        $this->db->where('key', 'config_template');
        $this->db->where('value', $this->input->post('config_template')); // Need to update theme row 
        $query = $this->db->update('setting');
    }
}
<<p> 视图/strong>
<form method="post" action="<?php echo $action;?>" role="form" class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label" for="input-template"><?php echo $entry_template; ?></label>
<div class="col-sm-10">
<select name="config_template" id="input-template" class="form-control">
<?php foreach ($templates as $template) { ?>
<?php if ($template == $config_template) { ?>
<option value="<?php echo $template; ?>" selected="selected"><?php echo $template; ?></option>
<?php } else { ?>
<option value="<?php echo $template; ?>"><?php echo $template; ?></option>
<?php } ?>
<?php } ?>
</select>
<br />
<img src="" alt="" id="template" class="img-thumbnail" />
</div>
</div>
<button type="submit" class="btn btn-md btn-primary">Save</button>
</form>
控制器

public function index() {
        $this->load->model('setting/model_setting');
        $this->model_setting->updateTheme();
        if(null !==($this->input->post('config_template'))) {
            $data['config_template'] = $this->input->post('config_template');
        } else {
            $data['config_template'] = $this->theme->get('value'); // Auto loaded Library Theme
        }
        $data['templates'] = array();
        $directories = glob(DIR_CATALOG . 'views/theme/*', GLOB_ONLYDIR);
        foreach ($directories as $directory) {
            $data['templates'][] = basename($directory);
        }
        $this->form_validation->set_rules('config_template', '', 'callback_validate');
        if($this->form_validation->run()) {
            redirect('setting/store');
        } else {
            $this->lang->load('setting/setting', 'english');
            $data['breadcrumbs'] = array();
            $data['breadcrumbs'][] = array(
                'text' => $this->lang->line('text_home'),
                'href' => site_url('common/dashboard')
            );
            $data['breadcrumbs'][] = array(
                'text' => $this->lang->line('heading_title'),
                'href' => site_url('setting/setting')
            );
            $data['action'] = site_url('setting/setting');
            $data['title'] = "Settings";
            $data['entry_template'] = $this->lang->line('entry_template');
            $data['header'] = $this->header($data);
            $data['footer'] = $this->footer($data);
            $this->load->view('setting/setting', $data);
        }
    }

您应该先阅读一些基本的CI:

function index() {
    $this->load->model('setting/model_setting');    //load model
    if( $this->input->post(null) ){ //detect if form is submitted
        if(null !==($this->input->post('config_template'))) {
            $data['config_template'] = $this->input->post('config_template');
        } else {
            $data['config_template'] = $this->theme->get('value'); // Auto loaded Library Theme
        }
        if($this->form_validation->run()) {
            $this->model_setting->updateTheme();
        }
        redirect('setting/store');
    }else{                          // load view only
        $data['templates'] = array();
        $directories = glob(DIR_CATALOG . 'views/theme/*', GLOB_ONLYDIR);
        foreach ($directories as $directory) {
            $data['templates'][] = basename($directory);
        }
        $this->form_validation->set_rules('config_template', '', 'callback_validate');
        $this->lang->load('setting/setting', 'english');
        $data['breadcrumbs'] = array();
        $data['breadcrumbs'][] = array(
            'text' => $this->lang->line('text_home'),
            'href' => site_url('common/dashboard')
        );
        $data['breadcrumbs'][] = array(
            'text' => $this->lang->line('heading_title'),
            'href' => site_url('setting/setting')
        );
        $data['action'] = site_url('setting/setting');
        $data['title'] = "Settings";
        $data['entry_template'] = $this->lang->line('entry_template');
        $data['header'] = $this->header($data);
        $data['footer'] = $this->footer($data);
        $this->load->view('setting/setting', $data);
    }
}

您应该首先检查是否有任何post请求,如果有更新和重定向,否则显示表单

我已经让它工作了。

模型
<?php
class Model_setting extends CI_Model {
    public function update_theme() {
        $this->db->where('key', 'config_template');
        $this->db->set('value', $this->input->post('config_template'));
        $this->db->update('setting');
    }
}
控制器

public function index() {
        //$this->load->model('setting/model_setting');
        $data['templates'] = array();
        $directories = glob(DIR_CATALOG . 'views/theme/*', GLOB_ONLYDIR);
        foreach ($directories as $directory) {
            $data['templates'][] = basename($directory);
        }
        $this->form_validation->set_rules('config_template', '', 'callback_validate');
        if($this->form_validation->run() == true) {
            $this->load->model('setting/model_setting');
            if($this->model_setting->update_theme()) {
                $data['config_template'] = $this->input->post('config_template');
            } else {
                $data['config_template'] = $this->input->get('config_template');
            }
            redirect('setting/store');
        } else {
            $this->lang->load('setting/setting', 'english');
            $data['breadcrumbs'] = array();
            $data['breadcrumbs'][] = array(
                'text' => $this->lang->line('text_home'),
                'href' => site_url('common/dashboard')
            );
            $data['breadcrumbs'][] = array(
                'text' => $this->lang->line('heading_title'),
                'href' => site_url('setting/setting')
            );
            $data['action'] = site_url('setting/setting');
            $data['title'] = "Settings";
            $data['entry_template'] = $this->lang->line('entry_template');
            $data['header'] = $this->header($data);
            $data['footer'] = $this->footer($data);
            $this->load->view('setting/setting', $data);
        }
    }