这是我的函数
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');
}
}
速记更新很好,但无论我做什么,车站和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();
}
我认为这是你需要的。请根据您的需要进行管理
$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);
各位评论的人,谢谢,但我已经解决了这个问题 在控制器里面,我忘了在这行后面放别的
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');
}
}
}
感谢您的阅读