为什么我必须单击刷新按钮才能在提交后从数据库中获取新数据

时间:2013-09-15 08:04:20

标签: php codeigniter

我面临提交表单的问题,我有一个视图,显示来自数据库的数据和每个记录旁边的编辑按钮,弹出div与表单来更新该特定记录。问题是,当我提交表单并将数据保存在数据库中时,视图将显示包含旧数据的记录,除非我点击浏览器中的刷新按钮。 (codeigniter缓存设置为FALSE)

控制器:     

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

class Product extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('Table');
        $this->load->library('DX_Auth');

        // $this->load->library('pagination');
        $this->load->helper('form');
        $this->load->helper('url');

        // Protect entire controller so only admin, 
        // and users that have granted role in permissions table can access it.
        $this->dx_auth->check_uri_permissions();

        $this->load->model('Product_model');
        $this->load->library('Form_validation');
    }

    function index($sort = 0, $offset = 0) {
        $data['product'] = $this->Product_model->get_all()->result_array();

        if (isset($_POST['save'])) {
            $this->form_validation->set_rules('link', 'Link', 'trim|required|xss_clean|prep_url');
            $this->form_validation->set_rules('video', 'Video', 'trim|required|xss_clean|prep_url');
            if ($this->form_validation->run() === TRUE) {
                $post = $this->input->post();
                if(!isset($post['link_target_blank'])){
                    $post['link_target_blank'] = 0;
                }
                unset($post['save']);
                unset($post['id']);
                $this->Product_model->update_settings($this->input->post('id'), $post);
            }
        }

        // Load view
        $header_data['title'] = 'Game Product';
        $this->load->view($this->config->item('header_view'), $header_data);
        $this->load->view($this->config->item('blocks_view') . 'head_block');
        $this->load->view($this->config->item('menus_view') . 'main_menu');
        $this->load->view('backend/product', $data);
        $this->load->view($this->config->item('footer_view'));
    }

}

?>

1 个答案:

答案 0 :(得分:2)

因为你的获取请求:

$this->Product_model->get_all()->result_array(); 

在插入/更新请求之前:

$this->Product_model->update_settings($this->input->post('id'), $post);

因此,当你得到所有数据时,数据还没有。

如果你将if语句放在if语句之后 - 那么它应该可以工作,例如:

if (isset($_POST['save'])) 
{
    // all your POST processing and saving...
}

$data['product'] = $this->Product_model->get_all()->result_array();