这是我的职能
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都不会更新。但是,如果我删除了这段代码。
$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 {
所有字段将更新。你能看看我的密码吗?预先感谢。
这是我的更新查询
function update($data, $id){
$this->db->where('location_id', $id);
$this->db->update('locations', $data);
return $this->db->affected_rows();
}
答案 0 :(得分:1)
我认为这是您所需要的。请根据您的需要进行管理
$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 :(得分:0)
有很多人发表评论,谢谢,但是我已经解决了这个问题 在控制器内部,我忘了在此行之后放置
if($this->form_validation->run() == FALSE){
redirect('locations');
}
这就是整个功能的外观
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');
}
}
}
感谢阅读