CodeIgniter Forums
Prevent double insert at same time - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Prevent double insert at same time (/showthread.php?tid=92321)

Pages: 1 2


Prevent double insert at same time - pippuccio76 - 01-14-2025

Hi , today i check if there are some record inserted at same time in  a db's table and i have 21 record duplicated 8 on 2022, 12 on 2023 and 1 on 2024 .I   reduce this effect by adding a js working when i push submit button (disabling button and showing a gif)  at half of 2024 but i have  a duplicate on december 2024 . 

How can I completely avoid this problem?


RE: Prevent double insert at same time - michalsn - 01-14-2025

Use redirect() after sending the form: https://codeigniter.com/user_guide/outgoing/response.html#redirect


RE: Prevent double insert at same time - pippuccio76 - 01-14-2025

(01-14-2025, 09:18 AM)michalsn Wrote: Use redirect() after sending the form: https://codeigniter.com/user_guide/outgoing/response.html#redirect

Always i redirect after submit , this is an example :

Code:
$db = \Config\Database::connect();

$db->transStart()

//my code to insert

$db->transComplete();

if($db->transStatus()!==false) {

    session()->setFlashdata('gestisciRecordOK', 'MESSAGE OK');

    return redirect()->to('/CONTROLLER/METHOD/');


}else{

    session()->setFlashdata('gestisciRecordBad', 'MESSAGE BAD');

    return redirect()->to('/CONTROLLER/METHOD/');
}

But this is not the solution...


RE: Prevent double insert at same time - michalsn - 01-14-2025

1. What do you mean by duplicated records?
2. Are you sending a form?
3. Are you using JS to send form data?


RE: Prevent double insert at same time - pippuccio76 - 01-16-2025

1 same data at same seconds
2 yes with post
3 only to form submit()


RE: Prevent double insert at same time - michalsn - 01-16-2025

Please show me how you're handling the submission process in JS.


RE: Prevent double insert at same time - cha - 01-16-2025

using a js client side verification isn't efficient nor secure.

to prevent the form being submited twice with same data one way is to use CI4 CSRF security

set Config/Security.php $regenerate = true
put a CSRF token in your form
set a CSRF filter on your POST route or check CSRF token in your controller on POST

https://codeigniter.com/user_guide/libraries/security.html

you may also set the db column(s) as unique so if one user try to submit dupplicate data with a valid CSRF the db will not accept them


RE: Prevent double insert at same time - pippuccio76 - 01-16-2025

1 same data at same seconds
2 yes with post
3 only to form submit()


RE: Prevent double insert at same time - michalsn - 01-16-2025

You posted the same answer as 4 hours ago. Please show me your actual JS code that handles form submission.


RE: Prevent double insert at same time - pippuccio76 - 01-16-2025

(01-16-2025, 04:48 AM)michalsn Wrote: You posted the same answer as 4 hours ago. Please show me your actual JS code that handles form submission.

sorry i make a mistake :

button to submit
Code:
        <div class='text-right mt-4'>
        <span>note: * campi obbligatori</span><br>
        <a href='<?= base_url() ?>' class='btn btn-danger'>Annulla</a>
        <button class='btn btn-info' id='bottone_invio_form' type='button' onclick=''>INSERISCI</button>
        <span id="gif_animata">  <i class="fas fa-spinner fa-spin"></i></span>
        </div>

script
Code:
        $("#bottone_invio_form").click(function() {

            var scegli_materiali = $("#scegli-materiali").val();

            var quantita = $("#id_class_quantita").val();

            if(scegli_materiali == ""  ){

                alert('Scegli un codice!!!!');

            }else if(quantita ==''){
               
                alert('Inserisci una quantità');


            }else{

                $("#bottone_invio_form").hide();

                $("#gif_animata").show();

                $("#myform").submit();

            }

         

        });