Forms codeigniter表单编辑错误修复

Forms codeigniter表单编辑错误修复,forms,codeigniter,Forms,Codeigniter,在Codeigniter中,在编辑表单时,如果任何字段中发生任何错误,则在表单提交后,在数据库instaof update中会出现新条目。我怎样才能修好它 这是我的控制器代码: item()方法用于编辑页面 function item() { //pre process for edit $page_id = $this -> uri -> segment(3); if ($page_id != false) { //chec

在Codeigniter中,在编辑表单时,如果任何字段中发生任何错误,则在表单提交后,在数据库instaof update中会出现新条目。我怎样才能修好它

这是我的控制器代码: 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);
        }
    }
}
函数进程(){
//编辑前处理
$page_id=$this->input->post('id');
//回显“页面ID:”.$页面ID;
如果($page_id!=false){
//检查有效的页面id
$page\u q=$this->customer\u code\u m->get\u by\u id($page\u id);
如果($page\u q->num\u rows()!=0){
//有效页面id
$page\u data=$page\u q->row();
}否则{
//假页面id
重定向(“欢迎”);
}
}
//加载表单验证
$this->load->library('form_validation');
//设置有效性规则
$this->form_validation->set_rules('CustomerCode'、'Customer Code'、'trim | required | prep_for_form');
$this->form_validation->set_规则('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_规则('Name_Of_Propritor'、'Name Of Propritor'、'trim | required | prep_for_form');
$this->form_validation->set_rules('u Propritor_BN的名称','Propritor Bangla的名称','trim | required | prep_for_form');
$this->form_validation->set_规则('Address'、'Address'、'trim | prep_for_form');
$this->form_validation->set_rules('Address_BN','Address Bangla','trim | prep_for_form');
$this->form_validation->set_规则('Phone'、'Phone'、'trim | integer | prep_for_form');
$this->form_validation->set_规则('Fax'、'Fax'、'trim | prep_for_form');
$this->form_validation->set_rules('Mobile'、'Mobile'、'trim | integer | required | prep_for_form');
$this->form_validation->set_规则('Email'、'Email'、'trim | valid|u Email | prep|u for|u form');
$this->form_validation->set_规则('Division'、'Division'、'trim | required | prep_for_form');
$this->form_validation->set_规则('Area'、'Area'、'trim | required | prep_for_form');
$this->form_validation->set_规则('Zone'、'Zone'、'trim | required | prep_for_form');
$this->form_validation->set_规则('Type_Of_Party'、'Type Of Party'、'trim | required | prep_for_form');
$this->form_validation->set_rules('genr_Of_Party'、'genr Of Party'、'trim | required | prep_for_form');
$this->form_validation->set_规则('Grade_Of_Party'、'Grade Of Party'、'trim | required | prep_for_form');
$this->form_validation->set_规则('Status'、'Status'、'trim | prep_for_form');
如果($this->form\u validation->run()==FALSE){
$this->item();
}否则{
$form\u data=array('CustomerCode'=>htmlspecialchars($this->input->post('CustomerCode')),'Name\u Of_Customer'=>htmlspecialchars($this->input->post('Name\u Of_Of___-BN'),'Name\u Of_Of_-Propritor'=>htmlspecialchars($this->input->input->('Name'u Of'u Propritor'),'Name'u Of'u Propritor'u BN'=>htmlspecialchars('this->input->post('Name'u Of'u Propritor'u BN'),'Address'=>htmlspecialchars('this->input->post('Address'),'Address'BN'=>htmlspecialchars('this->input->input->post('Phone')),'Fax'=>htmlspecialchars('($this->input->post('Fax')),'Mobile'=>htmlspecialchars($this->input->post('Mobile'),'Email'=>htmlspecialchars($this->input->post('Division')),'Area'=>htmlspecialchars($this->input->post('Area'),'Zone'),'Zone'=>htmlspecialchars($this->input->post('Zone')),“党的类型”=>htmlspecialchars($this->input->post($Type\u-Of-Party”),“党的类型”=>htmlspecialchars($this->input->post($general\u-Of-Party)),“党的等级”=>htmlspecialchars($this->input->post($Grade\u-Of-Of-Party));
如果($this->input->post('Status')==1){
$form_data['Status']=1;
}否则{
$form_data['Status']=0;
}
$data['form_data']=$form_data;
如果(!空($page_data)){
$form_data['page_id']=$page_data->id;
$this->customer\u code\u m->update($form\u data);
//设定味精
$this->session->set_flashdata('page_msg','

已成功更新。

'); 重定向(“客户\代码/详细信息\项目/”$page\数据->ID); //打印(表格数据); }否则{ $this->customer\u code\u m->create($form\u data); //设定味精 $this->session->set_flashdata('page_msg','

已成功创建。

'); 重定向(“客户代码/全部”); //打印(表格数据); } } }
要解决您的问题,您应该将页面id保存在会话变量中。您可以使用

$this->load->library('session');
然后像这样保存页面

$this->session->set_userdata('page_id', $this->input->post('ID'));
对于其余的代码,您可以像这样使用session变量

$this->session->userdata('page_id');
编辑:


使用
flashdata
只会使一个请求的会话保持活动状态。将会话数据保存在数据库中是完全不同的事情。在您的情况下,不需要使用flashdata,但将数据保存在会话表中是更好的安全实践,因为通常会话保存在cookie中。您可以使用该方法。这在两种情况下都适用你使用
flashdata
或普通会话。这两件事在我的评论中混淆了。我希望你现在清楚了。

如果有错误,就不应该提交表单。你能再解释一点吗?我的问题是,我处理每个有表单错误的字段('fieldname')的异常在我看来。但在编辑页面提交时,如果我遗漏了任何require字段,错误会像往常一样发生