updating user account balance php mysql
I want to create a website where users will have accounts that will have funds that can be spent or withdrew. By the way I am using a mySql db.
Lets say a user has $10 in their account and they place an order for $5.
Could I just user the UPDATE statement to update the balance? Also how would I be able to log the order into the transaction history?
Really? You want to build a website that handles "money" with absolutely no tools to work with?
Ok - yes is the answer to your question. You could use an update query to change the user's balance as well as to write a transaction record.
Run with it.
Originally Posted by ginerjm
What tools would you be referring to?
How could make the update to subtract only the amount that was entered in the form. Such as if a user has $20 in their account and they make a withdrawal. I only want to subtract the amount they withdrew.
The basic query format could be something like:
But then you would want additional logic/queries to make sure that balance won't drop below zero (presumably?).
UPDATE table_name SET balance = balance - :withdrawl_amount WHERE user_id = :user_id
But somewhat to what ginerjm was referring to: If you are going to actually be taking/sending real money from users, you better have some real strong knowledge about what you are doing, including all the security issues and legal requirements -- not just throwing together a bunch of PHP/MySQL code that you hope will work. Payment systems on sites like Paypal and Amazon were not thrown together in a week by some guy sitting up late at his computer: they were built by teams of developers, testers, subject matter experts, etc.; because if you do that sort of thing wrong, you can be totally screwed.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
OK. Now I have it working how I wanted but I wanted the balance to be displayed like this: 1.0000 or 0.0000. I tried changing the type to INT, Float, Decimal, and Double but neither of them displayed the balance how I wanted it to be. Instead if the balance is 1 it will display "1" instead of "1.0000". Does anyone know how I would be able to make that happen?
What would be the best way to make sure a balance doesn't go below zero?
Originally Posted by NogDog
You do not seem to have got the message: You cannot just set up a system handling real money. I'm no expert, but as far as I am aware:
- If you are a trader, wishing to take payments for individual transactions by debit/credit card and or Paypal, you have to use their plug-in to handle the payment process. And meet whatever criteria they set.
- If you want clients to have the facility to deposit funds (which may be used to buy goods) then you are, in effect, offering banking facilities. To do that, you have to be vetted and approved by the relevant authorities. Well, in most countries that is the case. That will include anti-money-laundering conditions. E.g. You can face multi-million dollar fines and/or imprisonment for not having adequate checks on the identities of customers, and/or the sources of the money they deposit with you!
- If you allow credit facilities (intentionally or otherwise) on which interest is charged, that comes under consumer credit legislation, and there are stiff penalties for illegal money lending not to mention usury...
Writing a payment system in PHP is the least of your worries!!! You probably need legal advice.
Last edited by jedaisoul; 06-05-2014 at 03:46 AM.
What Nogdog said!
Originally Posted by ytesfay80
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Tags for this Thread