首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Codeigniter,字段未更新,但其他字段将更新

Codeigniter,字段未更新,但其他字段将更新
EN

Stack Overflow用户
提问于 2019-12-17 14:00:11
回答 2查看 48关注 0票数 2

下面是我的函数

代码语言:javascript
复制
function store(){
            $this->load->library('form_validation');
            $this->form_validation->set_rules('shorthand','Shorthand','required|is_unique[locations.shorthand]');

            if($this->form_validation->run() == FALSE){
                redirect('locations');
            } else {
            $id = $this->input->post('location_id');
            $location_name = ucwords(strtolower($this->input->post('location_name')));
            $shorthand = strtolower($this->input->post('shorthand'));
            $station = ucwords(strtolower($this->input->post('station')));

            $data = array(
                'location_name'=>$location_name,
                'shorthand'=>$shorthand,
                'station'=>$station
            );

            }if($id){
                $result = $this->location_model->update($data,$id);
                if($result > 0){
                    $this->session->set_flashdata('success', 'Update successfull!');
                }else{
                    $this->session->set_flashdata('error', 'Failed to update!');
                }
                redirect('locations');
            }else{
                $result = $this->location_model->create($data);
                if($result > 0){
                    $this->session->set_flashdata('success', 'Location added!');
                }else{
                    $this->session->set_flashdata('error', 'Failed to add location!');
                }
                redirect('locations');
            }
        }

速记更新很好,但是无论我做什么,station和location_name都不会更新。但如果我删除这段代码。

代码语言:javascript
复制
$this->load->library('form_validation');
            $this->form_validation->set_rules('shorthand','Shorthand','required|is_unique[locations.shorthand]');

            if($this->form_validation->run() == FALSE){
                redirect('locations');
            } else {

所有字段都将更新。你能看看我的代码吗?提前谢谢。

下面是我的更新查询

代码语言:javascript
复制
function update($data, $id){
            $this->db->where('location_id', $id);
            $this->db->update('locations', $data);        
            return $this->db->affected_rows();
        }
EN

回答 2

Stack Overflow用户

发布于 2019-12-17 14:44:20

我想这就是你需要的。请根据您的需要进行管理

代码语言:javascript
复制
$locations = $this->db->get_where('locations', array('location_id' => $id))->row();
$original_value = $locations->shorthand; 
if($this->input->post('shorthand') != $original_value) {
   $is_unique =  '|is_unique[locations.shorthand]';
} else {
  $is_unique =  '';
}

$this->form_validation->set_rules('shorthand','Shorthand','required|trim|xss_clean'.$is_unique);
票数 1
EN

Stack Overflow用户

发布于 2019-12-17 14:41:18

对于那些评论过的人,谢谢你们,但是我已经解决了这个问题,它是我忘记放在这行后面的控制器内部。

代码语言:javascript
复制
if($this->form_validation->run() == FALSE){
                redirect('locations');
            }

下面是整个函数现在的样子

代码语言:javascript
复制
function store(){
            $this->form_validation->set_rules('location_name','Location_name','required');
            $this->form_validation->set_rules('shorthand','Shorthand','required|callback_locationExists');
            $this->form_validation->set_rules('station','Station','required');

            $id = $this->input->post('location_id');
            $location_name = ucwords(strtolower($this->input->post('location_name')));
            $shorthand = strtolower($this->input->post('shorthand'));
            $station = ucwords(strtolower($this->input->post('station')));

            $data = array(
                'location_name'=>$location_name,
                'shorthand'=>$shorthand,
                'station'=>$station,
            );
            if($this->form_validation->run() == FALSE){
                redirect('locations');
            }else{
                if($id){
                    $result = $this->location_model->update($data,$id);
                    if($result > 0){
                        $this->session->set_flashdata('success', 'Update successfull!');
                    }else{
                        $this->session->set_flashdata('error', 'Failed to update!');
                    }
                    redirect('locations');
                }else{
                    $result = $this->location_model->create($data);
                    if($result > 0){
                        $this->session->set_flashdata('success', 'Location added!');
                    }else{
                        $this->session->set_flashdata('error', 'Failed to add location!');
                    }
                    redirect('locations');
                }
            }
        }

感谢您的阅读

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59368361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档