PHP

Moderators: None (Apply to moderate this forum)
Number of threads: 1847
Number of posts: 5013

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
PHP and Database Question Posted by Codeisle on 15 Feb 2011 at 3:59 PM
I'm working on this PHP script and I have a problem I'm trying to avoid: keeping database record accuracy, especially on a website with many active users. For example, keeping the number of entries into a newsletter database limited to 100 emails.

I assume that, theoretically, several 1000 people could enter their email and press submit on a form at around the same time, so doing a check for entry count in the database may be inaccurate. The check may be untrue, because someone else (user b) could be added to the database after a check (for user count) and before an update (adding user a). I thought of using optimistic locking as a solution. But the way I understand it, with optimistic locking, I may have to display an error if the data was previously updated by another user submission (checking the timestamp). An error message is not very desirable for the end user, so I thought of this pseudocode using the idea of optimistic locking but without an error message everytime the timestamp has changed:

sub adduser(tries) {

if all tries are exhausted {

display error 

} else {

check if entries are less than the max defined in the database

if max was reached {

display a message to user informing them no more entries may be submitted

} else {

get current timestamp

...do stuff

check timestamp with one stored in the database

If timestamp was changed by another submission before the update { 

call adduser(tries-1) (recursively, to hopefully get a timestamp that is unaltered)

} else {

add user's email to list in db

update timestamp in the database

display a message telling the user that the submission was successful
}
}
}

This is just a basic idea I had (I know a lot is missing from the code, but my only concern is keeping the count under control).

Does anyone think this is a solid solution (or that I'm at least on the right track), or is there a better way to deal with this?



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.