I have developed a with Php and Mysql. As more people are working on it simultaneously, there is the danger that two users have the same form on the screen, and one user changes fields, while the other is then working on old information.

When this latter submits, he will destroy the changes that the first user made. Is there a neat way to prevent this? Blocking certain pages, making a warning? How does that work in PHP and/or Mysql?

Recommended Answers

All 11 Replies

Well as an example if you're using a database you can add a column to the table you're dealing with called "locked" that is set to the editing user's ID when they open the form, then set back to 0 when that same person saves it. If someone with a different user ID tries to save or open it will block them.

Well as an example if you're using a database you can add a column to the table you're dealing with called "locked" that is set to the editing user's ID when they open the form, then set back to 0 when that same person saves it. If someone with a different user ID tries to save or open it will block them.

Thanks. Except, my question has more to do with the simultaneous use of variables. How can a scripter avoid a situation where the first user's input get redefined by a second user before the first user's data gets saved to a database?

Thanks. Except, my question has more to do with the simultaneous use of variables. How can a scripter avoid a situation where the first user's input get redefined by a second user before the first user's data gets saved to a database?

Actually that's the problem i was solving. Unless you're constantly forcing the user to update while they are editing (obtrusive) they're always going to have old data. The only way to prevent them from modifying old data after someone else updated is to implement locking.

Actually that's the problem i was solving. Unless you're constantly forcing the user to update while they are editing (obtrusive) they're always going to have old data. The only way to prevent them from modifying old data after someone else updated is to implement locking.

Isn't there a difference between locking a database and locking a variable? And if so, how will locking a record keep another user from redefining the first users input variable a split second before it's inserted into a database??

Isn't there a difference between locking a database and locking a variable? And if so, how will locking a record keep another user from redefining the first users input variable a split second before it's inserted into a database??

Part A) You're not actually locking the database, you're soft-locking, ie., setting a field on a row saying that it is currently in use.

Step 1: User 23 goes to edit Test4
----------------------
id | name  | locked
----------------------
5  | Test4 | 23        <-- Locked by user 23
----------------------

Step 2: User 25 goes to edit Test4
Error: Test4 is currently being edited blah blah

Step 3: User 23 saves Test4 and leaves the page
----------------------
id | name  | locked
----------------------
5  | Test4 | 0         <-- Unlocked
----------------------

Step 4: User 25 goes to edit Test4
----------------------
id | name  | locked
----------------------
5  | Test4 | 25        <-- Locked by user 25
----------------------

Part B) Locking the record keeps the other user from redefining it because you check for a lock before saving, if it's locked you don't save and you alert the user that it is locked.

I understand. So, how do you think user 25's data will be temporarily saved while user 23 does their thing?

Likewise, how will 23's data be saved temporarily is 25 gets in first?

I just started to read about PHP Sessions. Won't PHP Sessions take care of this issue?

I understand. So, how do you think user 25's data will be temporarily saved while user 23 does their thing?

Likewise, how will 23's data be saved temporarily is 25 gets in first?

I just started to read about PHP Sessions. Won't PHP Sessions take care of this issue?

It wouldn't be temporarily saved, they'd have to wait until it's unlocked and redo their modifications.

If you're not using sessions already how are you handling user authentication or is it just open to the whole world?

It wouldn't be temporarily saved, they'd have to wait until it's unlocked and redo their modifications.

If you're not using sessions already how are you handling user authentication or is it just open to the whole world?

I wasn't taking care of it. That's what I need to fix. Sounds like Sessions is the direction I need to go. Please let me know if you have another idea.

Thanks!

Member Avatar for diafol

Interesting - @ Shawn - what happens if somebody is working on a record and then goes away / closes the computer without saving? Will the db value still have 1 in the locked column? Sorry - I'm messing with table locking at the moment and looking for a 'simple' method myself.

Interesting - @ Shawn - what happens if somebody is working on a record and then goes away / closes the computer without saving? Will the db value still have 1 in the locked column? Sorry - I'm messing with table locking at the moment and looking for a 'simple' method myself.

Implementing a timer wouldn't be too hard, setting it to the same length as the session timeout would be the most straightforward way.

Member Avatar for diafol

YEP.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.