2013-05-30 42 views
0

在Codeigniter中,表單編輯時,如果在任何字段中發生任何錯誤,則在表單提交後,它會在數據庫instate更新中出現新條目。我該如何解決它?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); 
     } 
    } 
} 
+0

如果出現錯誤,則不應提交表格。你能多解釋一下嗎? – sakibmoon

+0

我的問題是,在我看來,我用form_error('fieldname')處理每個字段的異常。但在編輯頁面提交時,如果我錯過了任何require字段,錯誤就像往常一樣發生,但是我失去了page_id,這就是爲什麼當我嘗試再次提交表單時,它會創建一個新的頁面ID並在數據庫中創建新的提交。現在我的問題是我怎樣才能保存我的page_id爲一個特定的提交? –

+0

使用會話。張貼我的答案。 – sakibmoon

回答

0

要解決您的問題,您應該將page_id保存在會話變量中。你可以與

$this->load->library('session'); 

會話然後保存這樣

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

的PAGE_ID對於代碼的其餘部分,可以使用會話變量這樣

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

編輯:

使用flashdata將只保留s對於一個請求,活動仍然活着。將會話數據保存到數據庫中是完全不同的事情。在你的情況下,不需要使用flashdata,但將數據保存在會話表中更好的安全實踐,因爲通常會話保存在cookie中。你可以使用它。無論您使用flashdata還是普通會話,這都適用。這兩件事在我的評論中混淆了。我希望你現在清楚。

+0

如果我使用$ this-> session-> set_flashdata('page_id',$ this-> input-> post('ID'))是否會通過使用set_userdata()來解決問題或其他安全問題? –

+0

它將數據保存在數據庫中,您必須創建表使其工作。檢查'保存數據庫中的會話數據'部分:http://ellislab.com/codeigniter/user-guide/libraries/sessions.html – sakibmoon

+0

沒有我的問題是,我正在創建一個會話。所以如果我使用session-> set_flashdata(「---」),它的工作原理是否一樣? –