2017-10-16 96 views
0

這是我的插入函數。它插入數據並在另一個視圖中顯示數據。但它有一個問題。如果我刷新它再次插入的視圖頁面。我想要防止再次插入刷新頁面。 plzzzzzzzzzzzzz如何停止插入數據到數據庫刷新頁面使用codeigniter

視圖

<html> 
<head></head> 
<body> 
<form method="post" action="insert_controller"> 
<input type="text" placeholder="plate number" name="plate"> 
<input type="text" placeholder="maker" name="maker"> 
<input type="text" placeholder="model" name="model"> 
<input type="text" placeholder="year" name="year"> 
<input type="text" placeholder="color" name="color"> 
<input type="text" placeholder="drive" name="drive"> 
<input type="text" placeholder="phone" name="phone"> 
<input type="text" placeholder="name" name="name"> 
<input type="text" placeholder="city" name="city"> 
<input type="text" placeholder="country" name="country"> 
<input type="submit" name="submit"> 
<a >cancel</a> 
</form> 
</body> 
</html> 

這是我controller.After插入數據,其結果是好的,到searching_view頁。但是當我刷新了searching_view頁面時,它再次插入數據。我想要避免再次插入數據來刷新searching_view頁面。

控制器

class car extends CI_Controller { 
    public function insert_controller() { 
     $data['search']=$this->m->insert_model(); 
     $this->load->view('searching_view',$data); 
    } 
} 

它的模型。它插入數據,並從數據庫中獲取數據,並將結果發送。

模型

class add_model extends CI_Model { 
    public function insert_model() 
    { 
    if ($this->input->post('submit')) 
    { 

    date_default_timezone_set('Asia/Karachi'); 
    $date=date('g:i A').' on '.date('F d,Y'); 
    // echo "<pre>"; 
    // die(print_r($date, TRUE)); 
    $plate = $this->input->post('plate'); 
    $phone = $this->input->post('phone'); 
    $this->db->select('*'); 
    $this->db->from('car'); 
    $this->db->where('plate', $plate); 
    $this->db->or_where('phone', $phone); 
    $d = $this->db->get(); 
    if ($this->db->affected_rows()>0) 
    { 
     redirect('http://localhost/Ci/index.php/Car/error_message2'); 
     } 
     else 
    { 
     $arr= array(
       'plate' => $this->input->post('plate'), 
       'maker' => $this->input->post('maker'), 
       'model' => $this->input->post('model'), 
       'year' => $this->input->post('year'), 
       'color' => $this->input->post('color'), 
       'drive' => $this->input->post('drive'), 
       'phone' => $this->input->post('phone'), 
       'name' => $this->input->post('name'), 
       'city' => $this->input->post('city'), 
       'country'=> $this->input->post('country'), 
       'date' => $date 
       ); 
        $this->db->insert('car', $arr); 

        $id= $this->db->insert_id(); 

        // fetching 

        $this->db->select('id,plate,maker,model,year,color,drive,phone,name,city,country'); 
        $this->db->from('car'); 
        $this->db->where('id', $id); 
        $d = $this->db->get(); 
        return $d->result(); 
     } 
    } 


    } 
} 
+0

您的插入和列表方法應該不同。我的意思是在插入完成後,應該使用'redirect'而不是加載'searching_view' – jagad89

回答

2

那麼通常瀏覽器會警告你,你發送另一個POST請求前。當試圖

  1. 進行重定向
  2. (使用CSRF-tokens

第二種解決方案將返回錯誤:但是,如果你想完全避免這種可能性,你可以做以下的事情重新發送數據,所以不應該在沒有重定向的情況下使用,因爲用戶在重新加載時只會遇到錯誤。

相關問題