Avatar of SonnyRey
SonnyRey
 asked on

preventing update of customer balance twice

I have a web application using coldfusion and mysql. There is a form where
customer's payment is entered and submitted. Upon submission customer's
balance is updated. Sometimes customer's balance is updated twice.
How do I prevent this from happening?  

Example:
Balance:  1000
payment: 100
After update: balance = 800
 
Thanks.
MySQL Server

Avatar of undefined
Last Comment
SonnyRey

8/22/2022 - Mon
johanntagle

Without seeing your code, it's hard to make specific suggestions.  But one solution is to generate a unique identifier, something like a transaction or order id.  When the user clicks submit, the application should check if payment has already been made for this id.  If so, then it has found a duplicate payment.
SonnyRey

ASKER
how do you make a unique id?  Cannot make a unique id based on time.
johanntagle

You can just use an integer primary key column + auto_increment
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
SonnyRey

ASKER
This is my code for updating the balance.

<cfquery name="updBal" datasource="#dsn#">
       update cust
       balance = balance - #form.payment#
</cfquery>

<cfquery name="addToTransact datasource="#dsn#">
     insert into transact
     (name, payment, pay_date)
     values
     ('#form.name#', '#form.payment#', #Now()#)
</cfquery>

Transact table and cust table have primary id (transactid and custid)

Sometimes this piece of code is executed twice.  When that happens transactid
is incremented twice for the two records inserted.

How would a primary key come into play?

Thanks.
ASKER CERTIFIED SOLUTION
johanntagle

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SonnyRey

ASKER
That might work. Let me try it.
Thanks.