How to show the added data in php array as negative

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
			]);

Any help is appreciated

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 for your reply.
I want to store it as negative.
where would I do the multiply it by -1 ?

Just before you store it. What happens if you put it in the code you posted?

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

What is the column definition for the amount field?

name: amount
Type:TINYINT
Length: 3
is that what you’re asking?

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

Thanks for your replies. Much appreciated.
I tried your first suggestion and that seemed to work: -$amount

thanks again

1 Like

For posterity, the original suggestion by @droopsnoot was to change the code to this:

$insert_buy = $db->insert('transact', [
    'id_user' => $user_id,
    'amount' => -1 * $amount
]);

Yes, I hadn’t thought that just sticking a minus sign would work.