Jump to content

two users updating same record


rohithreddyk

Recommended Posts

Dont know if the solution is particular to PHP..But wanted to post here anyway coz I am developing the site in PHP..

 

 

I am doing a website where users can update the records in edit.php page..

src=C:\Documents and Settings\rkkodaka\D[/img]

 

Problems is...suppose two users have opened the edit.php at the same time.. suppose the comments field is balnk at first. Now user1 enters "hi..this is SAM" in the comments field and clik submit button. Database is updated with that field. Now user2, unknowing that user1 added some comment, enters "hi this is MARK" in the comments field and clicks submit button.. now the comment entered by user1 is lost.. i want the user2 to get a notification like "some other user already updated the comments field after you opened the page".."do you want to continue"? if he hits Ok update it.. if he hits cancel dont update

Link to comment
Share on other sites

If I follow you right, you are just wanting to have one comment show up at a time, and only one row in the database...

 

When someone opens the page run a query and check the ID field of the row or something of that nature.

 

Then when the user hit's submit you could run another query that checks if the ID is equal to the same ID as before, if so contiue with the posting, if not echo out "do you want to continue"?

Link to comment
Share on other sites

You should add timestamp columns to your table.  I like to call mine created_tm and modified_tm.  Each time you insert you set them both equal to now(), or the current time.  From then on each time you update one you set modified_tm equal to now(), or the current time.  This is best done with a trigger so you don't have to program it in your application code.

 

When you present data to the user, include an:

<input type="hidden" name="modified_tm" value="<?php echo $row->modified_tm;?>" />

<input type="hidden" name="primary_key" value="<?php echo $row->primary_key;?>" />

 

Then in your processing script you can do something like:

$moditm = escape_user_input( $_POST['modified_tm'] );

$pk = escape_user_input( $_POST['primary_key'] );

$count = "select count(*) from table where primary_key={$pk} and modified_tm>{$moditm}";

if( $count > 1 ) {

  // record has been updated since user first saw it, so prompt

}else{

  // record has NOT been updated since user first saw it, so update

}

 

That's pseudo code to outline the basic design so don't copy and paste it and expect it to work.  And if you don't want the user to be able to manipulate the hidden fields, then don't send them to the client as hidden fields but instead store them in a session or similar mechanism.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.