0

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

1 回答 1

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无论您是使用会话还是正常会话,这两种情况都适用。这两件事在我的评论中混淆了。我希望你现在清楚了。

于 2013-05-30T20:08:08.077 回答