【问题标题】:duplicate insert after refresh page (iam new in developing with codeigniter)刷新页面后重复插入(我是使用 codeigniter 开发的新手)
【发布时间】:2015-05-23 18:59:14
【问题描述】:

我需要在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 具有 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
             }
           }


}

【问题讨论】:

    标签: codeigniter codeigniter-2 codeigniter-url


    【解决方案1】:

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

    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');
       }
    }
    

    【讨论】:

    • 它返回错误致命错误:不能在 C:\wamp\www\ 中对函数调用的结果使用 isset()(您可以使用“null !== func()”代替)第 9 行的 speed\application\controllers\speed.php
    • codeigniter.com/userguide2/libraries/form_validation.html试试这个文档,它会告诉你如何验证表单是否成功提交,然后将数据添加到数据库中
    猜你喜欢
    • 2010-10-19
    • 2011-04-13
    • 2017-03-16
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    • 1970-01-01
    • 2020-10-06
    相关资源
    最近更新 更多