刷新页面后重复插入(使用codeigniter进行开发时的新功能)

时间:2015-05-23 18:59:14

标签: codeigniter codeigniter-2 codeigniter-url

我需要在codeigniter刷新页面新的时候停止插入数据,并且不能轻易地重定向,请支持 view:pages / home.php视图,其中有一个表单

<html>
<head></head>
</body>
<?php echo validation_errors(); ?>
<?php echo form_open('speed/insert_to_db'); ?>

Branch: <input type="text" name="branch" /><br/>
Business Unit: <input type="text" name="buinessUnit" /><br/>
Device Type: <input type="text" name="deviceType" /><br/>
Brand: <input type="text" name="brand" /><br/>
Device Model: <input type="text" name="deviceModel" /><br/>
SN: <input type="text" name="SN" /><br/>
status: <input type="text" name="status" /><br/>
department: <input type="text" name="department" /><br/>
username: <input type="text" name="username" /><br/>
notes: <input type="textarea" name="notes" /><br/>
computername: <input type="text" name="computerName" /><br/>
Save:<input type="submit" name="save" />

</form>
</body>
</html>

model:add_model model,其中包含insert_to_db函数

       public function insert_into_db(){
           $post=$this->input->post();

           $data=array('Branch'=>$post['branch'],'BusinessUnit'=>$post['buinessUnit'],'DeviceType'=>$post['deviceType'],'Brand'=>$post['brand'],'DeviceModel'=>$post['deviceModel'],'SN'=>$post['SN'],'Status'=>$post['status'],'Departmant'=>$post['department'],'UserName'=>$post['username'],'Notes'=>$post['notes'],'ComputerName'=>$post['computerName']);
           $this->db->insert('hardware_assets', $data);
return $this->db->insert_id(); // if using mysql
       }
}

控制器:

<?php

class Speed extends CI_Controller {

        function insert_to_db()
           {

             $this->load->model('add_model');
             if($this->input->post('save')){
             $this->add_model->insert_into_db();
             $this->load->view('pages/home');//loading success view
             }
           }


}

1 个答案:

答案 0 :(得分:0)

我认为你应该在你的控制器中尝试这个:

function insert_to_db()
{
   $this->load->model('add_model');

   if( isset( $this->input->post('save') ) )
   {
      $this->add_model->insert_into_db();

      //loading success view
      $this->load->view('pages/home');
   }
}