0

Code similar to this is executed when a form is saved:

$wpdb->query("DELETE FROM Unit_list WHERE group = '$group_id'");
$order = 1;
$unique_units = array_unique($units);
foreach ($unique_units as $unit) {
    $wpdb->insert("Unit_list", array("unit" => $unit, "group" => $group_id, "sort_order" => $order));
    $order++;
}

A few times a day there would be some duplicates.

If the form was submitted twice with enough time for this part of the code to run each time there would be no duplicates because the second time would involve the whole first group being deleted.

For there to be duplicates this code must be running twice at the exact same time.

Double-clicking quickly would do it but if a third click was slow and not simultaneous the group would get deleted and not involve duplicates.

After working out that double clicking was the cause, the solution is to stop subsequent clicks of the form submission button from being detected.

Luke Wenke
  • 1,149
  • 2
  • 23
  • 43
  • This is not a theoretical site. – RiggsFolly Jul 14 '16 at 00:28
  • 2
    They could be clicking on the Submit button twice. – Barmar Jul 14 '16 at 00:29
  • 1
    Check the log to see if there are multiple calls to the URL. – Barmar Jul 14 '16 at 00:29
  • The form submission goes back to the same URL when updating a user (sometimes leads to duplicates in the ordered items [an ordered list of units]) but there are different URL parameters when creating users. – Luke Wenke Jul 14 '16 at 00:44
  • 1
    @LukeWenke perhaps you don't "think" it's a double-click but you don't *know* until you read your web server logs. It's all guesswork until you do. Correlate, then confirm or disprove, please. – Michael - sqlbot Jul 14 '16 at 02:03
  • Sorry for some reason I thought people meant reloading the page while it was submitting rather than clicking the submit button multiple times. I'm using an almost identical version of IE11 and I'm seeing if I can duplicate it by clicking submit multiple times. – Luke Wenke Jul 14 '16 at 02:26
  • Ok it can be caused by repeatedly clicking submit. Well I couldn't get 2 versions of the ordered lists to interleave (requires the code to be running perfectly simultaneously) but I could tell that the database was being used multiple times (based on the differences in the indexes - multiple deletes were used) – Luke Wenke Jul 14 '16 at 02:36
  • How exactly do you think we can help you without *any details at all*? You've included no code, no information about the database, and no logs. – skrrgwasme Jul 14 '16 at 02:39
  • Despite that people led me to the answer - that it was multiple clicks – Luke Wenke Jul 14 '16 at 02:45

2 Answers2

1

See a client-side (jQuery) solution here: Prevent double submission of forms in jQuery

And a PHP one here: http://phpsense.com/2006/prevent-duplicate-form-submission/

Community
  • 1
  • 1
ldg
  • 9,112
  • 2
  • 29
  • 44
  • Thanks... the code uses a complex $('#editUserForm').submit(function(e){ function that involves return false (for validation problems) or $form.submit();.... It began with e.preventDefault(); but I recently commented that out for some reason... – Luke Wenke Jul 14 '16 at 03:00
  • 1
    So I just added $('#submit-form-button').prop('disabled', true); before $form.submit(); – Luke Wenke Jul 14 '16 at 03:54
0

It seems the problem is people are clicking multiple times. The php code updating the database runs several times if I click on the submit button several times...

But...

I am unable to duplicate the problem of ordered database entries being interleaved. I suspect it would require the mouse clicks to be within a few milliseconds of each other. Perhaps there is a problem with their mouse or browser causing double click events to fire off. It happens for particular users multiple times over the course of days.

Luke Wenke
  • 1,149
  • 2
  • 23
  • 43