PHP and Database Question - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

PHP and Database Question

CodeisleCodeisle Posts: 2Member
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:

[code]
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
}
}
}
[/code]
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?
Sign In or Register to comment.