I’m not an ant, but I think the first thing you should do is to locate a section of existing code that takes an item of data and puts it into a table. The code you posted builds up an array as it shows, but it does not include the part that posts it into a database table, that must be somewhere else.
Once you’ve identified that code, have a read of it and see whether you can figure out how to write the required data into another separate table.
What does this mean? What happens, or doesn’t happen? Presumably you’ve checked what values are in those variables at the point that you have used them for the insert, and that they are suitable for the columns you are inserting them into?
Slightly O/T, but what’s the idea of storing the user-id and user-name in this extra table? Surely you can get the username using a JOIN if you need it? Duplicating data isn’t a good idea for all sorts of reasons.
Much thanks again.
Without success means no data was displayed in the ‘transact’ table > ‘wallet’ column.
However, the file always successfully adds the ‘wallet’ amount to the ‘users’ table. The ‘wallet’ column in 'users table and the ‘wallet’ column in ‘transact’ table have the same structure, if that’s what you’re asking when you ask if they are ‘suitable’.
Yes, the other two variables are correct for their respective columns, too.
Sorry, I’m not clear on your ‘presumably’ question.
Currently, this populates ‘wallet’ and ‘username’ columns, in ‘transact’ table:
however, the amount that appears in ‘wallet’ column in ‘transact’ table is always one number less that the amount that appears in the ‘wallet’ column of the ‘users’ table.
I’m not sure why.
I look forward to any further assistance.
I mean - are the values in those variables correct at the point that you have used them for the insert? Or are you presuming that they must be, because you use one or all of them somewhere else in the code? The fact that it now almost works when you use other variables suggest that this might have been contributing to the problem.
OK, so it’s basically working now then? Or does it leave $user_id blank? If it does, what is the value of that variable at the point you call the function? If it is leaving it blank, I’d suggest that you should alter the table layout to not allow it, as it seems like an important column.
By “one number less”, do you mean that the value has decreased by 1 (i.e. that in one table it stored for example 5, but in the transact table it stores 4), or that it has one fewer digits, i.e. in one table it stores 45.23, but in the other it stores 45.2? If it’s the latter, I seem to recall you did some work on the number of decimal places you were storing numbers to, in another thread.
If it’s the former, all you can do is look at the code that runs between where it is inserted into the users table and where you insert it into the transact table. Something must be altering the value.
I’m sorry, but I’m not clear on part of your reply, it sounds like you’re saying alter the table to not allow something important.
Additionally, the amount entered into the html form field replaces whatever is in the ‘wallet’ column, with each form submit. Ultimately, I’m looking to modify it so that (instead of the entered amount replacing the amount in the ‘wallet’ column) the entered amount adds to (or subtracts) whatever is in the ‘wallet’ column. But, I’m not sure if I should start another posting for that help.
That completely misses the point of transactions. Do you think your bank has a column somewhere with your balance in it? They don’t. What they do have is a long list of all transactions that occurred on your account and then the sum of all those makes up your balance. Sure, they may cache some of it to speed things up, but that’s how these things work.
If you only keep the value you can never explain why it has that value. If you keep all transactions you can.
Also, why is the wallet used from $_POST? That suggests that if I modify the value in the form (which is easy using modern day developer tools) I can get stuff for less than their actual price?
Thanks for your reply/insight.
I agree with what you’re saying.
Maybe I’m explaining my modify request unclearly, but I’m trying to get assistance to modify so I “have is a long list of all transactions that occurred…”, as you have described (better than me) that’s what the ‘transact’ table is for. Currently, it only displays the balance/total amount in its ‘wallet’ column. I’d like a field to add or subtract so users > wallet has ongoing balance and ‘transact’ > wallet has each transaction. Any help with that is what I’m looking for.
Regarding, “also, why is the wallet used from $POST…” I’m sure it important to know that the Form we’re discussing only appears to Admin via the Admin panel.
I look forward to additional comments/suggestions.
I was suggesting that you modify the table to not allow you to post a new row where the id_user column is blank. That way, your whole insert would fail, rather than allowing invalid data to be stored, presuming your site would consider a transaction with no user-id to be invalid.
So not terribly successfully then. What is the value of $user_data->user_id just before you run this new bit of code? Not what should it be, but what is it when you display it or log it in your debug log? This is what I was asking before - what’s in the variables you use? You keep changing them.
For the first part, you can modify the code so that you retrieve the current balance, add the new figure to it, and write it back, or you can do it within the query. I don’t know the database class you’re using, whether it’s possible to do it via a query. For the second part, that’s what your code here should do, once you figure out why the user-id value is blank.
Please, please, please - don’t call them both “wallet”, that will be confusing to anyone else who has to work with the system, especially if one “wallet” column contains a balance, and the other contains a transaction amount. Give them proper, meaningful names.
Thanks again for the helpful replies.
So, I have gotten this
'id_user' => $user_data->id,
to populate the id_user column with the correct user id.
Now, it is my understanding that I need to “retrieve the current balance, add the new figure to it, and write it back”, so, I believe you mean something like the code below, plus the html entered value = the new wallet balance? something like that?:
$db = "SELECT wallet FROM users";
but, I don’t know how to tie it back to what’s entered into the html form:
You’re heading in the right direction. Think about that query though - you will need to specify which wallet you need to retrieve - you don’t want all entries in that table, you just want one specific one.
You already have code to take the information from the html form, and put it into a variable, because your code is writing it into two tables now. You also know how to put a value back into a table using an UPDATE query, because you’re doing that as well. You must have code somewhere that adds the value of two variables together because your whole system has transactions and balances all over the place. You already have all the building blocks.
There’s also the note from @rpkamp about whether it’s a good idea to store a balance in your table. It’s surprisingly easy for this to go out of sync with the sum of the transactions. You might want to read up on MySQL transaction handling.
No, they must read up on MySQL transaction handling before you start doing this. We’re talking about people’s money here. There is no such thing as trying some stuff and hope for the best when it comes to actual money are paying for your product. That stuff must be correct. At least to keep a good reputation for your product, if nothing else.
The more I think about this the more I think it’s a really bad idea to implement this in the current code. There is no encapsulation of any kind in the code, so you can be fairly certain this transact table is being accessed in multiple spots throughout the code. Just forgetting to change one spot can have disastrous consequences.
Unless the architecture of transactions is improved I would strongly recommend OP against doing this.
The fact that the software doesn’t seem to have any automated tests doesn’t help either.
Also, the OP has never answered the question as to why they want this. If we knew this we might be able to provide alternate solutions.
Some confusion in the column names there - is there a column called wallet and a column called walletbalance in your users table? If not, I’m not sure where $wallet comes from.
In any case, you can
newvalue = value of new transaction
update users set walletbalance = walletbalance + newvalue where user_id = ?
all in a single query. That probably reduces the chance of the scenario that @rpkamp talks about, but you should really tie it into a transaction, which will mean that if for some reason the wallet transaction isn’t stored, then the balance won’t be updated either.