繁体   English   中英

CodeIgniter更新无法正常工作

[英]CodeIgniter Update not Fully Working

我在尝试使用CodeIgniter时更新mySQL表。

控制器

我已注释掉redirect()以使用print_r()。 返回数组时,将返回正确的更新值。 但是,如果我取消注释重定向,那么它将把我重定向到带有表的页面,并且值将不会更新。 我还要检查phpMyAdmin,以确保值不会被更新,而不仅仅是显示,而且它们中的任何一个都不会更新。 这使我感到困惑,因为print_r()返回正确的值。

<?php
class update_ctrl extends CI_Controller {
    function __construct(){
        parent::__construct();
        $this->load->model('update_model');
    }

    public function updateGame($id){
        $this->load->model('update_model');
        $data['games'] = $this->update_model->getGame($id);
        $this->load->view('update_view', $data);
        $this->load->view('footer');
    }

    public function update(){
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<span class="error">', '</span>');
        $this->form_validation->set_rules('name', 'Name', 'trim|required');
        $this->form_validation->set_rules('genre', 'Genre', 'trim|required');
        $this->form_validation->set_rules('developer', 'Developer', 'trim|required');
        $this->form_validation->set_rules('year', 'YearReleased', 'trim|required|numeric');
        $this->form_validation->set_rules('price', 'Price', 'trim|required|numeric');
        $id = $this->input->post('ID');
        $data = array(
                                'Name' => $this->input->post('name'),
                                'Genre' => $this->input->post('genre'),
                                'Developer' => $this->input->post('developer'),
                                'YearReleased' => $this->input->post('year'),
                                'Price' => $this->input->post('price')
                        );
            $this->load->model('update_model');
            $this->update_model->update($id, $data);
            $this->session->set_flashdata('msg', 'Game Updated!');
            print_r($data);
            //redirect('');
        }
    }
?>

该模型

<?php
class update_model extends CI_Model {
    public function getGame($id) {
        $this->db->select('*');
        $this->db->from('games');
        $this->db->where('ID', $id);
        $query = $this->db->get();
        if ($query->num_rows() > 0){
            return $query->result();
        } else {
            return $query->result();
        }
    }

    public function update($id, $data){
        $this->db->where('ID', $id);
        $this->db->update('games', $data);
    }
}
?>

我唯一能想到的是您的查询无提示地失败了。 进入database.php并将db_debug更改为TRUE并再次运行所有内容。

我还要补充一点,您似乎在确保所有字段都经过验证方面做得很好。 我还要添加一个id验证,以确保您得到它-如果用户执行了一些时髦的操作,则由于未设置id而不会出现一些奇怪的查询错误。

注释掉所有$this->form_validation->set_rules然后重试,我认为数值验证会出错

 public function update(){
    $this->load->helper(array('form', 'url'));
    $this->load->library('form_validation');
    //$this->form_validation->set_error_delimiters('<span class="error">', '</span>');
    //$this->form_validation->set_rules('name', 'Name', 'trim|required');
    //$this->form_validation->set_rules('genre', 'Genre', 'trim|required');
    //$this->form_validation->set_rules('developer', 'Developer', 'trim|required');
    //$this->form_validation->set_rules('year', 'YearReleased', 'trim|required|numeric');
    //$this->form_validation->set_rules('price', 'Price', 'trim|required|numeric');
    $id = $this->input->post('ID');
    $data = array(
                            'Name' => $this->input->post('name'),
                            'Genre' => $this->input->post('genre'),
                            'Developer' => $this->input->post('developer'),
                            'YearReleased' => $this->input->post('year'),
                            'Price' => $this->input->post('price')
                    );
        $this->load->model('update_model');
        $this->update_model->update($id, $data);
        $this->session->set_flashdata('msg', 'Game Updated!');
        print_r($data);
        //redirect('');
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM