I have a form app that subtracts a value from a record in another table like this:
sc_exec_sql("UPDATE current_stock SET Amount = Amount - {Amount} WHERE ".$update_where);
I noticed sometimes (totally random), the value is taken twice. So to troubleshoot, I added code to set up and initialize a counter on ScriptInit and increment it by 1 within onValidateSuccess and log this. That means, after the line above, I have
[norecs] += 1;
Then I log the product id (Prod), Warehouse id (WH), value being subtracted and [norecs]. Please see the picture below.
Note at 14:30:33, this log was inserted, followed by SC’s standard log entry (which was logged twice, still don’t know why as per my previous issue at http://www.scriptcase.net/forum/foru…-sc_log)
Weird thing is 14secs later, it re-executes my troubleshooting logging code (which is highlighted in yellow.) Notice the [norecs] is still showing 1 which means it did not increment the [norecs].
But it subtracted 80 twice from the current_stock table!
Notice at 14:31:33 and 14:31:57, it works properly (i.e. troubleshooting log entry only once, subtracted only once though sc log entry is still twice).
The only safe way I’m thinking might be to do these manipulations as a stored procedure but I find them quite cumbersome so I’m hoping someone can give me other pointers before I explore that.
Thanks in advance.
PS. the subsection of onValidateSuccess looks like this:
<quote>
sc_exec_sql("UPDATE current_stock SET Amount = Amount - {Amount} WHERE ".$update_where);
[norecs] += 1;
$ip = $_SERVER[‘REMOTE_ADDR’];
$dt = date(‘Y-m-d H:i:s’);
sc_exec_sql(“INSERT INTO sc_log (inserted_date, username, application, creator, ip_user, action, description) VALUES (’$dt’,’[usr_login]’,‘form_app’,‘user’,’$ip’,‘insert’,‘Prod {ProductID}/WH {WarehouseID}/ Subtracted {Amount} for the [norecs] st/nd time’)”);
<unquote> [ATTACH=CONFIG]n69555[/ATTACH]