更改表单时不更改数据库中的值

时间:2014-07-02 11:28:12

标签: php codeigniter

当我单击是保存维护值时,如果单击是更改保存到数据库1,而0则为否。

但现在由于某种原因它被卡在1上,并且不会将配置维护行值从1更改为0

控制器

   <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Setting extends MX_Controller {

    public function __construct(){
      parent::__construct();
      $this->load->library('user');
      $this->load->library('form_validation');
      $this->lang->load('english', 'english');
        $this->lang->load('setting/setting', 'english');
        $this->load->model('setting/model_setting');
        if($this->session->userdata('logged_in') == FALSE) {
         redirect('/');
      }
    }

    public function index() {

        $data['title'] = $this->lang->line('heading_title');

        if (($this->input->server('REQUEST_METHOD') == 'POST') && $this->validate()) {

            $this->model_setting->editSetting('config', $this->input->post());

            redirect('store');
        }

        $data['heading_title'] = $this->lang->line('heading_title');

        $data['text_yes'] = $this->lang->line('text_yes');
        $data['text_no'] = $this->lang->line('text_no');

        $data['entry_maintenance'] = $this->lang->line('entry_maintenance');

        $data['help_maintenance'] = $this->lang->line('help_maintenance');

        $data['button_save'] = $this->lang->line('button_save');
        $data['button_cancel'] = $this->lang->line('button_cancel');

        $data['tab_general'] = $this->lang->line('tab_general');
        $data['tab_server'] = $this->lang->line('tab_server');

        $data['breadcrumbs'] = array();

        $data['breadcrumbs'][] = array(
            'text' => $this->lang->line('text_home'),
            'href' => site_url('dashboard')
        );

        $data['breadcrumbs'][] = array(
            'text' => $this->lang->line('heading_title'),
            'href' => site_url('setting')
        );

        $data['action'] = site_url('setting');

        if (null !==($this->input->post('config_maintenance'))) {
            $data['config_maintenance'] = $this->input->post('config_maintenance');
        } else {
            $data['config_maintenance'] = $this->input->get('config_maintenance');
        }

        $this->load->view('setting/setting', $data);
    }

    public function validate() {

    }
}

模型

<?php 
class Model_setting extends CI_Model {

    public function getSetting($group, $store_id = 0) {
        $data = array(); 

        $query = $this->db->query("SELECT * FROM " . $this->db->dbprefix . "setting 
            WHERE store_id = '" . (int)$store_id . "' AND `group` = " . $this->db->escape($group) . "
        ");

        foreach ($query->row() as $result) {
            if (!$result['serialized']) {
                $data[$result['key']] = $result['value'];
            } else {
                $data[$result['key']] = unserialize($result['value']);
            }
        }

        return $data;
    }

    public function editSetting($group, $data, $store_id = 0) {
        $this->db->query("DELETE FROM " . $this->db->dbprefix . "setting 
            WHERE store_id = '" . (int)$store_id . "' AND `group` = " . $this->db->escape($group) . "
        ");

        foreach ($data as $key => $value) {
            if (!is_array($value)) {
                $this->db->query("INSERT INTO " . $this->db->dbprefix . "setting 
                    SET store_id = '" . (int)$store_id . "', 
                    `group` = " . $this->db->escape($group) . ", 
                    `key` = " . $this->db->escape($key) . ", 
                    `value` = " . $this->db->escape($value) . "
            ");
            } else {
                $this->db->query("INSERT INTO " . $this->db->dbprefix . "setting 
                    SET store_id = '" . (int)$store_id . "', 
                    `group` = " . $this->db->escape($group) . ", 
                    `key` = " . $this->db->escape($key) .", 
                    `value` = " . $this->db->escape(serialize($value)) . ", 
                    serialized = '1'
                ");
            }
        }
    }

    public function editSettingValue($group = '', $key = '', $value = '', $store_id = 0) {
        if (!is_array($value)) {
            $this->db->query("UPDATE " . $this->db->dbprefix . "setting SET `
                value` = ". $this->db->escape($value) ." 
                WHERE `group` = " . $this->db->escape($group) . " 
                AND `key` = " . $this->db->escape($key) . " AND store_id = '" . (int)$store_id . "'");
        } else {
            $this->db->query("UPDATE " . $this->db->dbprefix . "setting SET `
                value` = ". $this->db->escape(serialize($value)) ." 
                WHERE `group` = ". $this->db->escape($group) ." 
                AND `key` = " . $this->db->escape($key) . " AND store_id = '" . (int)$store_id . "' 
                AND serialized = '1'");
        }
    }   

}

1 个答案:

答案 0 :(得分:0)

我不会肯定地说,但看起来你没有对editSettings函数的函数调用进行任何检查:

if($this->input->server('REQUEST_METHOD') == 'POST')

您应该添加网址变量还是“删除?”复选框以检查以确保将其设置为您要删除某些内容的构造。使用此函数调用,每次提交表单时,无论表单中的任何值如何,都会调用此editSettings函数并删除内容。