We help IT Professionals succeed at work.

How to control documents in a BD that can be edited by multiple users?


We have documents in a cloud software that can be edited by multiple users at the same time (invoices, quotes, etc...). We need to find a solution for this, since these documents can be inconsistent.

For now we considered Mutex, but we don't know if there are any other possibilities for doing this (Merging algorithms or anything else). Everything is being saved in a mysql database.

Watch Question

Stupid question - have you considered using Google Docs?



I think you misunderstood my question, I mean that the documents are not saved as files, they are DB tables (Invoice_header,Invoice_details, you know...). When you have a Form in PHP and you store it in a Database, and when there are multiple users doing the same, that's the reason of the question.

Sorry for not being so explicit.


So what I'm hearing is:

1. You have one DB table that contains data submitted from a web form.
2. Each form submission is a record in that DB table.
3. Each record can be edited by a user.
4. If you have more than one user editing the same record, you have problems where users overwrite each other's changes.
5. You have a custom solution for editing the records.

Is that a correct understanding of the problem that you are trying to solve?


Yep, that's the correct meaning of what I tried to say.

The solution, then, is to implement two new columns in your database table that are called something like "locked_on" (an integer field) and "locked_by" (also an integer field that points to the table containing your users).
The names can be up to you - those are just suggestions.

Make it a When a user goes to edit the record, update the record and set locked_by to the user's ID and set locked_on to the UNIX timestamp (the function to get this in PHP is simply time() without any parameters), and in your WHERE clause, specify both the record ID and also locked_on is null. The query should look something like this:

$query = "UPDATE <your table> SET locked_on=".time().",locked_by=$variable_containing_current_user_id WHERE record_id=$var_containing_record_id_to_edit AND locked_on IS NULL";

Run that query, and then check the number of affected rows. If the # of affected rows is 0, then it means that someone else already grabbed that record and is working on it because locked_on has a timestamp in it (it isn't null). If the # of affected rows is 1, then it means you've acquired a lock on that particular record, and the user is now free to edit it.

Once the user is finished editing it, clear the timestamp when the record is updated:

$query = "UPDATE <your table> SET <your other fields>, locked_on = NULL, locked_by = NULL WHERE record_id=$var_containing_record_it_to_save";

You may also have a situation where someone closes their browser without saving, and the locked_on/locked_by isn't cleared. That said, you can always clear the fields automatically if the locked_on timestamp is older than a certain # of seconds (e.g. 600 seconds or 10 minutes or however long it normally takes a user to work on a record). If you do this, then while the user's browser is active and editing that record, use AJAX to send regular updates to the database every couple of minutes, updating the value of locked_on to the current timestamp.

This way, someone can work on the record as long as they need to, as long as their browser is open and their internet connection is up. While they're working, their browser is telling the database that the lock is still good. If their computer crashes, they can even re-edit the same record as long as they still have the lock.

And once the user finishes, and the locked values are cleared, the record becomes available for someone else to grab it.

There are some additional things you can implement to make the solution more robust, but those are the basic concepts.
Top Expert 2011

Do you want to know who is Editing what? Or just you need to know if the current record is in Edit Mode by one user?
If yes you need to know who is editing, then, do you create DB-user for the users? Or you create one user that establishes every connection?