codeigniter表单编辑错误修复

时间:2013-05-30 19:18:47

标签: forms codeigniter

在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);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

要解决您的问题,您应该将page_id保存在会话变量中。您可以使用

开始会话
$this->load->library('session');

然后像这样保存page_id

$this->session->set_userdata('page_id', $this->input->post('ID'));

对于其余代码,您可以像这样使用会话变量

$this->session->userdata('page_id');

修改

使用flashdata只会使一个请求的会话保持活动状态。将会话数据保存在数据库中是完全不同的事情。在您的情况下,不需要使用flashdata,但是在会话表中保存数据是更好的安全实践,因为通常会话保存在cookie中。你可以用它。无论您使用flashdata还是正常会话,都会应用此选项。这两件事在我的评论中变得混杂起来。我希望你现在明白了。