在 CodeIgniter 中插入重复数据

发布于 2024-12-16 00:37:43 字数 1592 浏览 3 评论 0原文

我只是在pastebin http://pastebin.com/KBtqrAkZ 的 codeigniter 控制器部分插入数据

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

比我的模型部分是简单添加在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 而不是插入数据。我是 codeigniter 的新手。请帮助我。任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏了南城 2024-12-23 00:37:43

这就是双重提交问题

有几种处理方法:

  1. 帖子/重定向/获取模式:破坏后退按钮,并且它不会阻止您的用户返回足够远以再次提交。不处理多次点击。

  2. 禁用提交按钮:有时会处理多次点击,但不会修复用户返回并再次提交的问题。

  3. 在会话中存储令牌:如果如果您在浏览器中打开了多个选项卡,则存储在会话中的令牌可能会混淆。 (注意:可以创建特定于浏览器选项卡的cookies使用javascript,但我自己没有尝试过。)

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

  5. 唯一交易 ID:在此 PHP 黑客页面,以及 此答案

  6. 会话中存在多个令牌:选项 3 的变体。如果将所有生成的令牌存储在会话中,则不需要涉及数据库。鉴于令牌在会话内是唯一的,重复的概率要低得多。可能的问题包括代币数量增长失控。也许可以通过有限大小的堆栈来修复,在该堆栈中,您将添加到堆栈的顶部,而额外的令牌会从底部掉下来。未经测试。

--

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

  1. 生成随机 transaction_id 并将其放入您的 Web 表单中。当用户点击submit时,它就会发生。

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

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

  4. 如果表中确实存在该id,则事务已完成。

您还应该搜索 [double-submit-prevention] 看看是否可以找到更好的解决方案。

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.

囚你心 2024-12-23 00:37:43

有一个简单的解决方案,您可以在添加产品后重定向到其他页面,例如:

redirect(base_url(). "yourcontrollername/index");

这样做将删除发布数据,并且数据不会重新添加到数据库中。

Theres a simple solution, you can redirect to some other page after adding product, like:

redirect(base_url(). "yourcontrollername/index");

Doing this will remove the post data and data would not be re-added to database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文