Gabriel Syme
Gabriel Syme

Reputation: 445

How to handle ajax drag and drop reordering of data

I am worried I am not doing this correctly. Currently I have implemented a drag and drop interface for a table of database data using jQuery UI's drag and drop functionality. On updating the order, I have a callback function make an ajax call sending the element's position to a php script. Based on the element's position, php recalculates the position of all the other items and updates their positions in the MySQL database.

My fear is that a user could reorder the list too quickly and cause the items in the database to be updated by php before they were finished being updated from the previous request, causing incorrect data. Is this a problem? Should I implement some way to queue up ajax requests until the previous one is complete? I'm wondering if there is some best practice for updating a database with ajax and making sure that the update is complete before updating the same item again.

Upvotes: 0

Views: 701

Answers (1)

Jack
Jack

Reputation: 15872

It seems that the answer is dependent on the implementation of your MySQL database. If you're using InnoDB then use Transactions.

Hopefully this thread will provide a solution to your problem. How to deal with concurrent updates in databases?

Upvotes: 1

Related Questions