Solved

preventing update of customer balance twice

Posted on 2012-03-18
6
237 Views
Last Modified: 2012-06-21
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.
0
Comment
Question by:SonnyRey
  • 3
  • 3
6 Comments
 
LVL 24

Expert Comment

by:johanntagle
ID: 37734749
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.
0
 

Author Comment

by:SonnyRey
ID: 37734934
how do you make a unique id?  Cannot make a unique id based on time.
0
 
LVL 24

Expert Comment

by:johanntagle
ID: 37735484
You can just use an integer primary key column + auto_increment
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:SonnyRey
ID: 37736462
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.
0
 
LVL 24

Accepted Solution

by:
johanntagle earned 500 total points
ID: 37736491
transactid is the primary key for transact, and yet the insert statement above does not include that column, so I assume that has integer auto_increment in your definition.  I think what you need to do is to already insert a row to the transact table before processing payment (so payment column is currently null), note the generated transactid, then make it part of the form, maybe as a hidden field.

Then when the form is submitted, a check is made if the row with that transactid already has a value for payment.  If so, then it is considered a duplicate and nothing else happens.  If not, then the usual balance update should proceed.
0
 

Author Comment

by:SonnyRey
ID: 37736918
That might work. Let me try it.
Thanks.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

All XML, All the Time; More Fun MySQL Tidbits – Dynamically Generate XML via Stored Procedure in MySQL Extensible Markup Language (XML) and database systems, a marriage we are seeing more and more of.  So the topics of parsing and manipulating XM…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question