Solved

preventing update of customer balance twice

Posted on 2012-03-18
6
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Introduction In this installment of my SQL tidbits, I will be looking at parsing Extensible Markup Language (XML) directly passed as string parameters to MySQL 5.1.5 or higher. These would be instances where LOAD_FILE (http://dev.mysql.com/doc/refm…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

761 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