在表单提交codeigniter上更新数据库

时间:2014-07-16 10:32:56

标签: php codeigniter

我无法在表单提交中更新数据库中的值。我使用codeigniter 2.20

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

我想要存档的内容是一旦选择了主题形式,它将更新设置表值,它将被发布到的位置。它也没有改变表单提交。 我有自动加载的form_validation lib和表单助手。

模型

<?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');
    }
}

查看

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

    }

2 个答案:

答案 0 :(得分:0)

我已经开始工作了。

模型

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

    }

答案 1 :(得分:0)

您应首先阅读一些基本的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);
    }
}

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