Page 1 of 1

php: prevent other users from saving data

Posted: Tue Jul 12, 2011 11:39 am
by cjkeane
hi everyone:

i have created a login script using sessions which works great. it displays the users sign on name on each page and i record their name when they save data into a mysql database.

i have an issue when two users (or more) save data at the same time. i have a page which allows users to transfer data from one table to another e.g. perform an insert from data from one table into another table, then delete the data from the original table. A problem occurs when two users have the same page open at the same time. If the first user transfers the data (the content then doesn't exist in the original table), then another user attempts to transfer the data, their name is inserted into the destination table with no other data as it doesn't exit in the original table. i need to prevent the second user from transferring data if it no longer exists. what is the best way to prevent this?

thanks.

Re: php: prevent other users from saving data

Posted: Tue Jul 12, 2011 11:53 am
by AbraCadaver
You should probably block anyone from opening that page if someone else has it open. When someone opens the page set a value in another DB table indicating that it is opened already and put a timestamp with it. Then if someone else tries to open it before a specified time after the timestamp (1 minute, 10 minutes, whatever) then don't let them. You should also check this again when performing the actual operation.

Re: php: prevent other users from saving data

Posted: Tue Jul 12, 2011 12:30 pm
by cjkeane
I was thinking about a timestamp or even inserting the logged on username into a new field called 'InUseBy' but then the issue occurs where i'd need to remove their name once they move to another record.

1. i'm not sure how to insert either a timestamp or their username into the table when the link is clicked
2. i'm not sure how to remove the timestamp/username from the table when they move to another record so another user can edit/save the record.

any helpful ideas?

Re: php: prevent other users from saving data

Posted: Tue Jul 12, 2011 4:56 pm
by McInfo
Use transactions. See mysqli::commit().

To make a script wait for another script's transaction to complete, you can use SELECT ... LOCK IN SHARE MODE.

Re: php: prevent other users from saving data

Posted: Tue Jul 12, 2011 8:29 pm
by cjkeane
yes i did read about lock in share mode. what's your opinion on SELECT ... FOR UPDATE?

Re: php: prevent other users from saving data

Posted: Tue Jul 12, 2011 9:13 pm
by McInfo
I don't have a good answer at this time.