在 Codeigniter 中,在编辑表单时,如果任何字段发生任何错误,则在表单提交后,它会在数据库中出现更新状态的新条目。我该如何解决?
这是我的控制器代码: item() 方法用于编辑页面。
function item() {
//pre process for edit
$page_id = $this -> uri -> segment(3);
if ($page_id != false) {
//check for valid package id
$page_q = $this -> customer_code_m -> get_by_id($page_id);
if ($page_q -> num_rows() != 0) {
// valid package id
$page_data = $page_q -> row();
$data['page_data'] = $page_data;
} else {
// fake package id
redirect("welcome");
}
}
$this -> load -> view('customer_code_v', $data);
}
process() 方法用于页面提交。
function process() {
//pre process for edit
$page_id = $this -> input -> post('ID');
//echo "PAGE ID:".$page_id;
if ($page_id != false) {
//check for valid page id
$page_q = $this -> customer_code_m -> get_by_id($page_id);
if ($page_q -> num_rows() != 0) {
// valid page id
$page_data = $page_q -> row();
} else {
// fake page id
redirect("welcome");
}
}
//loading form validation
$this -> load -> library('form_validation');
//seting valiadtion rules
$this -> form_validation -> set_rules('CustomerCode', 'Code Of Customer', 'trim|required|prep_for_form');
$this -> form_validation -> set_rules('Name_Of_Customer', 'Name Of Customer', 'trim|required|prep_for_form');
$this -> form_validation -> set_rules('Name_Of_Customer_BN', 'Name Of Customer Bangla', 'trim|required|prep_for_form');
$this -> form_validation -> set_rules('Name_Of_Propritor', 'Name Of Propritor', 'trim|required|prep_for_form');
$this -> form_validation -> set_rules('Name_Of_Propritor_BN', 'Name Of Propritor Bangla', 'trim|required|prep_for_form');
$this -> form_validation -> set_rules('Address', 'Address', 'trim|prep_for_form');
$this -> form_validation -> set_rules('Address_BN', 'Address Bangla', 'trim|prep_for_form');
$this -> form_validation -> set_rules('Phone', 'Phone', 'trim|integer|prep_for_form');
$this -> form_validation -> set_rules('Fax', 'Fax', 'trim|prep_for_form');
$this -> form_validation -> set_rules('Mobile', 'Mobile', 'trim|integer|required|prep_for_form');
$this -> form_validation -> set_rules('Email', 'Email', 'trim|valid_email|prep_for_form');
$this -> form_validation -> set_rules('Division', 'Division', 'trim|required|prep_for_form');
$this -> form_validation -> set_rules('Area', 'Area', 'trim|required|prep_for_form');
$this -> form_validation -> set_rules('Zone', 'Zone', 'trim|required|prep_for_form');
$this -> form_validation -> set_rules('Type_Of_Party', 'Type Of Party', 'trim|required|prep_for_form');
$this -> form_validation -> set_rules('Gener_Of_Party', 'Gener Of Party', 'trim|required|prep_for_form');
$this -> form_validation -> set_rules('Grade_Of_Party', 'Grade Of Party', 'trim|required|prep_for_form');
$this -> form_validation -> set_rules('Status', 'Status', 'trim|prep_for_form');
if ($this -> form_validation -> run() == FALSE) {
$this->item();
} else {
$form_data = array('CustomerCode' => htmlspecialchars($this -> input -> post('CustomerCode')), 'Name_Of_Customer' => htmlspecialchars($this -> input -> post('Name_Of_Customer')), 'Name_Of_Customer_BN' => htmlspecialchars($this -> input -> post('Name_Of_Customer_BN')), 'Name_Of_Propritor' => htmlspecialchars($this -> input -> post('Name_Of_Propritor')), 'Name_Of_Propritor_BN' => htmlspecialchars($this -> input -> post('Name_Of_Propritor_BN')), 'Address' => htmlspecialchars($this -> input -> post('Address')), 'Address_BN' => htmlspecialchars($this -> input -> post('Address_BN')), 'Phone' => htmlspecialchars($this -> input -> post('Phone')), 'Fax' => htmlspecialchars($this -> input -> post('Fax')), 'Mobile' => htmlspecialchars($this -> input -> post('Mobile')), 'Email' => htmlspecialchars($this -> input -> post('Email')), 'Division' => htmlspecialchars($this -> input -> post('Division')), 'Area' => htmlspecialchars($this -> input -> post('Area')), 'Zone' => htmlspecialchars($this -> input -> post('Zone')), 'Type_Of_Party' => htmlspecialchars($this -> input -> post('Type_Of_Party')), 'Gener_Of_Party' => htmlspecialchars($this -> input -> post('Gener_Of_Party')), 'Grade_Of_Party' => htmlspecialchars($this -> input -> post('Grade_Of_Party')));
if ($this -> input -> post('Status') == 1) {
$form_data['Status'] = 1;
} else {
$form_data['Status'] = 0;
}
$data['form_data'] = $form_data;
if (!empty($page_data)) {
$form_data['page_id'] = $page_data -> ID;
$this -> customer_code_m -> update($form_data);
//set msg
$this -> session -> set_flashdata('page_msg', '<p class="form_msg">Successfully updated.</p>');
redirect("customer_code/details_item/" . $page_data -> ID);
//print_r($form_data);
} else {
$this -> customer_code_m -> create($form_data);
//set msg
$this -> session -> set_flashdata('page_msg', '<p class="form_msg">Successfully created.</p>');
redirect("customer_code/all");
//print_r($form_data);
}
}
}