Duplicate data insertion in CodeIgniter

I just paste the data into the codeigniter controller part in 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 is partially added to 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 pasting. I am new to coderiger. Help me please. Any help would be greatly appreciated.

+3
source share
2 answers

This is a duplicate issue .

There are several ways to deal with it:

  • Post / Redirect / Get Sample : interrupts the back button and it does not keep your user far enough back to send it again. Doesn't handle multiple clicks.

  • Disable the submit button : it processes several clicks for a while, but does not fix the return and sending of the user.

  • Save the token in the session . If several tabs are open in the browser, the token stored in the session may become confused. (Note: it is possible to create a browser tab with specific cookies using javascript, but I have not tried this myself.)

  • Modify the database to not allow duplicates . The best solution, but also the greatest effort. If it detects a set of duplicate data, ignore the second query.

  • Unique Transaction Identifier . Described on this page PHP hacks on this answer .

  • Multiple tokens in a session : option of option 3. If you save all generated tokens in a session, you do not need to include a database. The likelihood of duplication is much lower, given that the tokens are unique within the session. Possible problems include a set of tokens that get out of hand. Maybe fixed with a limited stack size where you add to the top of the stack and additional tokens fall from the bottom. Unverified.

-

I like the unique transaction id method. It works as follows:

  • Create a random transaction_id and put it in your web form. This happens when the user clicks submit .

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

  • If the identifier does not exist in the table, complete the transaction and insert transaction_id in the table.

  • If the identifier exists in the table, the transaction has already been completed.

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

+18
source

Theres a simple solution, you can redirect to another page after adding the product, for example:

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

Doing this will delete the mail data and the data will not be re-added to the database.

+2
source

Source: https://habr.com/ru/post/947333/


All Articles