douhuo1738 2018-05-03 03:24
浏览 21

CodeIgniter更新不完全正常工作

I am trying to update a mySQL table while using CodeIgniter.

The Controller

I have the redirect() commented out to use print_r(). When it returns the array, it returns the correct updated values. But if i uncomment the redirect, then it will redirect me to the page with the table and the values will not get updated. I also check on phpMyAdmin to make sure values arent getting updated and not just displaying, but they arent updating on their either. This is confusing me because the print_r() is returning the correct values.

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

The Model

<?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);
    }
}
?>
  • 写回答

2条回答 默认 最新

  • douwen8118 2018-05-03 04:12
    关注

    The only thing I can think of is that your query is silently failing. Go into database.php and change db_debug to TRUE and run everything again.

    I would also like to add that you seem to do a good job making sure all your fields are validated. I'd also add a validation for id just to make sure you are getting it - in the case the user does something funky then they won't get some weird query error because the id wasn't set.

    评论

报告相同问题?

悬赏问题

  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?