This code below works successfully adding data to the correct db table column.
But, I’m trying to find a way to have the ‘amount’ appear as a negative in the myphpadmin table column named ‘amount’. Is it something I would tweak here:
// add data to transact table
$insert_buy = $db->insert('transact', [
'id_user' => $user_id,
'amount' => $amount
]);
Do you want it to just display as a negative number, or do you want to store it as a negative? You could just multiply it by -1 when you do whichever operation.
Thanks again.
I don’t know how to code it ‘just before you store it’ to ‘multiply it by -1’.
The code I posted stores in as a positive amount, currently
Yes. I was wondering if it was UNSIGNED and therefore couldn’t have negative values that might be part of the problem. But as long as the value can be -128 to 127 the sign problem isn’t a database problem.
It’s not quite clear what you want… is the issue that you have an amount $amount which is positive, and you want it inserted into the database as a negative value? If that is the case, then it should work to add a minus sign in front of the $amount in your insert statement, like so:
Notice the negative sign there towards the end… is that what you’re looking for?
If you want the amount to always be stored in the db as a negative value, but cannot trust that it arrives at that line as positive, then you can use `[ ‘id_user’ => $user_id, ‘amount’ => -abs($amount) ]
I’m not sure what you want… but maybe you are trying to write a SQL query which shows the amount as negative, even though it’s a positive value in the database?
If you want phpMyAdmin to show the value as the negative of the value that was inserted, you should write your query like this:
SELECT `id_user`, -`amount` FROM `transact` WHERE 1
If you want the amount to always show as negative, whether or not it’s negative in the database, then you’d do this in phpMyAdmin:
SELECT `id_user`, -ABS(`amount`) FROM `transact` WHERE 1