dsh8009271 2015-05-23 17:24
浏览 40

如何在使用codeigniter刷新页面时停止插入数据

i need to stop insert data when refresh page iam new in codeigniter and can't do redirect easly,please support view:pages/home.php view which has a form

<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 which has insert_to_db function

       public function insert_into_db(){
           $post=$this->input->post();
           if(!isset($post['save'])) return;
           $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
       }
}

controller:

<?php

class Speed extends CI_Controller {

        function insert_to_db()
           {

             $this->load->model('add_model');

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

           }


}
  • 写回答

2条回答 默认 最新

  • drvntaomy06331839 2015-05-23 17:35
    关注

    use $this->input->post('input_name') instead of raw code & check form submission in controller. i.e

    <?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
             }
    
    
           }
    }
    

    also remove if(!isset($post['save'])) return; from model

    评论

报告相同问题?

悬赏问题

  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘