Click here to Skip to main content
16,022,069 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a item and users apply amounts towards the total price. It works ok except if the item is $100 and previously users have applied $40 in total then they want to apply another amount of $80 I want only $60 to go for that item (insert) and the $20 remaining to stay in the balance. My attempt incorrectly shows in the insert (sorry math is not my strong suit).

(('-80' - (-80 - 100.00))




PHP
$applied = mysqli_real_escape_string($connect, $_POST['applied']);
$uid = mysqli_real_escape_string($connect, $_POST['uid']);

$updateStr = "select (ce.total - SUM(ad.g_price)) AS gross from       mytotal as ce JOIN transactions as ad ON ce.id=ad.pid where ce.uid ='$applied'";


$uQuery = mysqli_query($connect,$updateStr);
$totalArray = mysqli_fetch_array($uQuery, MYSQLI_ASSOC);




$updateStr = "INSERT INTO `transactions` (g_price, gross_price_float) VALUES (('$applied' - ($applied - $totalArray[gross_total])), '$applied')";

$updateQuery = mysqli_query($connect,$updateStr);


echo "<center><h2>Applied a balance</h2></center>";




What I have tried:

I have tried to adjust the insert my last attempt is above.
Posted
Updated 28-Aug-24 10:56am

1 solution

Without your DB there isn't a lot we can do to help you, but two things do spring to mind.
1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

2) That's some weird code: you appear to be using $applied as both an ID
PHP
$updateStr = "select... where ce.uid ='$applied'";
And as a mathematical value:
PHP
$updateStr = "INSERT INTO ... VALUES (('$applied' - ($applied - $totalArray[gross_total])), '$applied')";
But that's made even weirder because
Fragment
'$applied' - ($applied - $totalArray[gross_total])
will always be
$totalArray[gross_total]
anyway ...

I'd strongly suggest you start by thinking about what you are actually trying to do, and where things are stored! Then use the debugger to follow what your code is doing when it runs and watch what values to read / pass / end up with.
 
Share this answer
 
v2
Comments
mcbain19 29-Aug-24 7:56am    
I didn't parameterize this example but I used mysqli_real_escape_string to prevent malicious mysql inserts. I guess I was looking for general tips on how someone would normally apply amounts to a total and not exceed it from an open balance.
OriginalGriff 29-Aug-24 8:22am    
PHP has built in parameterisation:
https://medium.com/@ajay.monga73/parameterized-queries-php-guide-how-to-prevent-sql-injection-with-parameterized-queries-9899e77f9609
So use that always!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900