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.

9
  • This is not a theoretical site. Commented Jul 14, 2016 at 0:28
  • 3
    They could be clicking on the Submit button twice. Commented Jul 14, 2016 at 0:29
  • 1
    Check the log to see if there are multiple calls to the URL. Commented Jul 14, 2016 at 0: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. Commented Jul 14, 2016 at 0: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. Commented Jul 14, 2016 at 2:03

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

2 Comments

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...
So I just added $('#submit-form-button').prop('disabled', true); before $form.submit();
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.