Using PHP and MySQL I want to track sign ups using a PARENT and multi-CHILD relationship.
A problem occurs if more than 1 user attempts to sign up using the same parent code. The parent will only be credited for 1 user and not 2.
The solution as I understand it is to LOCK the record. Or use the server time and the record timestamp to determine last update.
#THIS IS NOT PRODUCTION CODE, BUT JUST INTENDED TO DEFINE MY PROCESS
#PLEASE ADVISE AND MAKE ADJUSTMENTS AS YOU SEE FIT
$sqlget = SELCT * record of PARENT and WHERE id is of PARENT
$ts -> timestamp;
$st -> servertime;
$af -> aff_number;
NOW I WANT TO CHECK THE LAST RECORD UPDATE OF THE TIMESTAMP OF THE PARENT,
IF THAT UPDATE OCCURRED LESS THAN 10 SECONDS AGO – I WANT TO RERUN my $sqlget
How do i Convert $ts to an INT and $st to an INT so that I can run an IF statement
to loop $sqlget IF my condition fails.
if($ts <= $st by 10 seconds) { wait 10 seconds and rerun sqlget }
else { get current $af (INT) and +1; UPDATE record; close connection;
META-refresh to next page; }
So does this process seem functional? Will this ensure that a PARENT is always properly credited for each child signup even if more than 1 child signs simultaneously?
I don’t expect multi-concurrent signups, but they may occur on a occasion and i must ensure that the PARENT is properly credited.
I do not understand your SQL structure.
If you associate a “credit” with the appearance of a new child with the same parent ID, then any number of users may affiliate simultaneously.
Just run:
and you’re done. It is indifferent whether parents and children are in separate tables or not. The important thing is that there must be no reference from the parent to the children, i.e., your only reference is the parent’s id in the child row. This way you have no concurrency problems on parent.
When you need to know how many children a given parent has got, simply COUNT them:
SELECT COUNT(*) FROM affiliations WHERE id_p = id_of_parent;
I suspect that you are now storing a counter in the parent row. If it is so, don’t: it looks simpler, but the concurrency problems make it not worth the effort.