重复数据插入CodeIgniter [英] duplicate data insert in CodeIgniter

查看:136
本文介绍了重复数据插入CodeIgniter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是在密码控制器部分将数据插入到pastebin中 http://pastebin.com/KBtqrAkZ

  public function add_product()
{
$ this-> lang-> load('log_in' , '英语');
log_in_check($ this-> lang-> line('log_in_authentication_error'),'admin / log_in');
$ this-> lang-> load('common','english');
$ data ['title'] = $ this-> lang-> line('admin_index_title');
$ this-> load-> view('admin_template / header',$ data);
$ this-> load-> view('admin_template / left_menu');
$ data ['error_msg'] ='';
if($ this-> form_validation-> run('add_product')=== TRUE)
{
$ this-> admin_model-> add_product($ this-> ;输入 - >柱());
$ this-> session-> set_flashdata('status_msg',$ this-> lang-> line('add_product_success'));
redirect(uri_string(),'refresh');
退出;
$ data ['error_msg'] = $ this-> lang-> line('add_product_invalid_data');
}
$ this-> load-> view('admin / add_product');
// $ this-> load-> view('admin_template / notification');
$ this-> load-> view('admin_template / footer');
}

比我的模型部分简单添加在pastebin http://pastebin.com/WiLHV2sr

  public function add_product($ data = array())
{
$ this-> db-> insert('ishop_product',$ data);
return $ this-> db-> insert_id();
}

我的问题是在重定向后,如果我按ctrl + F5或F5比数据正在插入。我是一个新的代号。请帮帮我。任何帮助将不胜感激。

解决方案

这是双重提交问题

有几种处理方法:


  1. Post / Redirect / Get模式 :打破后退按钮,它不会让您的用户回到足够远的位置再次提交。不能处理多次点击


  2. 禁用提交按钮:某些时候处理多个点击,但不修复用户返回并再次提交。


  3. 在会话中存储令牌 :如果浏览器中打开了多个选项卡,则存储在会话中的令牌可能会混淆。 (注意:可以创建浏览器标签cookie


  4. 将数据库更改为不允许复制:最好的解决方案,也是最大的努力。如果它检测到一组重复的数据,请忽略第二个请求。


  5. 唯一事务ID :描述在这个 PHP hacks页面,在这个答案


  6. 会话中的多个令牌:选项3上的变体。如果将所有生成的令牌存储在会话中,则不需要涉及数据库。由于令牌在会话中是唯一的,因此重复的概率要低得多。可能的问题包括令牌失控的令牌。也许可以用一个有限的大小的堆栈来固定,你添加到堆栈的顶部,额外的标记从底部掉下来。


-



我喜欢唯一事务ID 方法。它的工作原理如下:


  1. 生成一个随机的 transaction_id 并将其在您的网络表单。当用户点击提交


  2. 当您收到添加产品的请求时, 检查在事务表中的 transaction_id


  3. 如果表中不存在 ,请执行该事务,并将transaction_id插入到表中。


  4. 如果表中存在 id ,那么事务已经完成。


您还应该搜索 [双提交-prevention] ,看看能否找到更好的解决方案。


I am just inserting data in codeigniter controller part at pastebin http://pastebin.com/KBtqrAkZ

  public function add_product()
  {
    $this->lang->load('log_in', 'english');
        log_in_check($this->lang->line('log_in_authentication_error'), 'admin/log_in');
        $this->lang->load('common', 'english');
        $data['title'] = $this->lang->line('admin_index_title');
        $this->load->view('admin_template/header', $data);
        $this->load->view('admin_template/left_menu');
    $data['error_msg'] = '';
        if ($this->form_validation->run('add_product') === TRUE)
        {
      $this->admin_model->add_product($this->input->post());
            $this->session->set_flashdata('status_msg', $this->lang->line('add_product_success'));
            redirect(uri_string(), 'refresh');
      exit ;
          $data['error_msg'] = $this->lang->line('add_product_invalid_data');
        }
        $this->load->view('admin/add_product');
        //$this->load->view('admin_template/notification');
        $this->load->view('admin_template/footer');  
  }

Than my model part is simple add at pastebin http://pastebin.com/WiLHV2sr

  public function add_product($data = array())
  {
    $this->db->insert('ishop_product', $data);
    return $this->db->insert_id();
  }

my problem is after redirecting if I press ctrl + F5 or F5 than the data is inserting. I am a new in codeigniter. Help me please. Any help will be greatly appreciated.

解决方案

This is the Double Submit Problem.

There are several ways of dealing with it:

  1. The Post / Redirect / Get pattern: Breaks the back button, and it does not keep your user from going back far enough to submit again. Does not handle multiple clicks.

  2. Disable the submit button: Handles multiple clicks some of the time, but does not fix the user going back and submitting again.

  3. Store a token in the session: If you have multiple tabs open in the browser, the token stored in the session may get mixed up. (Note: It may be possible to create browser tab specific cookies using javascript, but I have not tried it myself.)

  4. Change the database to not allow duplicates: The best solution, but also the most effort. If it detects a set of duplicate data, ignore the second request.

  5. Unique transaction id: Described on this PHP hacks page, and on this answer.

  6. Multiple tokens in the session: A variation on option 3. If you store all generated tokens in the session, you don't need to involve the database. The probability of a duplicate is much lower, given that tokens are unique inside a session. Possible problems include the set of tokens growing out of control. Maybe fixable with a limited size stack where you add to the top of the stack, and extra tokens fall off the bottom. Untested.

--

I like the unique transaction id method. It works like this:

  1. Generate a random transaction_id and put it in your web form. It goes along when the user clicks submit.

  2. When you receive the request to add a product, check for the transaction_id in the transaction table.

  3. If the id does not exist in the table, do the transaction, and insert the transaction_id into the table.

  4. If the id does exist in the table, the transaction is already done.

You should also search for [double-submit-prevention] in to see if you can find an even better solution.

这篇关于重复数据插入CodeIgniter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆