I am inserting multiple records into a database. Many of the fields are consistent, but one field is calculated, and needs to calculate/subtract the value for each record inserted into the database.
<?php
$ExecuteQuery = new WA_MySQLi_Query($numeroseti);
$ExecuteQuery->Statement = "INSERT INTO transactions
(fromAcct, amount, fromAcctNewBalance, toAcctNewBalance, toAcct, description, timestamp)
(SELECT 1, ?, ?, balance + ?, acct_number, 'issuance', $time FROM users WHERE acct_number NOT LIKE '1800%' AND type <> 1 AND user_active = 1)";
$ExecuteQuery->bindParam("d", "".($_POST['issue_balance']) ."", "0"); //acct1num
$ExecuteQuery->bindParam("i", "".($getSender->getColumnVal("balance") - $amount) ."", "-1");
//acct2num
$ExecuteQuery->bindParam("i", "".($amount) ."", "-1");
//acct3num
$ExecuteQuery->execute();
?>
The field is fromAcctNewBalance
that I am having issues with. This is the code that calculates (like a ledger) what the new balance is:
$getSender->getColumnVal("balance") - $amount;
The problem is, it puts the same value into each record, when it should be subtracting the amount each time a new record is added.
For example, if the fromAcctNewBalance
starting balance is 600 and the amount issued to a user is 10, and next record that is added should have a fromAcctNewBalance
of 590, and so on, until it runs out of records to add to the table.
Does anyone have any ideas on how I can fix this issue?
I think the mistakes lies in the use of ->
inside the bindParam
.
So I tried to do the substraction without the bindParam
and $getSender->$getColumnVal - $amount;
simply doesn't work.
Use it like this
$getSender = $getColumnVal("balance") - $amount;
$ExecuteQuery->bindParam("i", "".($getSender) ."", "-1");
" ->
, is used when you want to call a method on an instance or access an instance property."
It is unclear to me, if your $getColumnVal("balance")
is a method or instance. If it is so, use it like this:
$getSender -> $getColumnVal("balance");
$getSender = $getSender - $amount;
$ExecuteQuery->bindParam("i", "".($getSender) ."", "-1");
References:
Where do we use the object operator "->" in PHP?
https://www.php.net/manual/de/language.types.object.php
EDIT:
Full code example
<?php
$ExecuteQuery = new WA_MySQLi_Query($numeroseti);
$ExecuteQuery->Statement = "INSERT INTO transactions
(fromAcct, amount, fromAcctNewBalance, toAcctNewBalance, toAcct, description, timestamp)
(SELECT 1, ?, ?, balance + ?, acct_number, 'issuance', $time FROM users WHERE acct_number NOT LIKE '1800%' AND type <> 1 AND user_active = 1)";
$ExecuteQuery->bindParam("d", "".($_POST['issue_balance']) ."", "0"); //acct1num
//Use the one that fits best
$getSender = $getColumnVal("balance") - $amount;
$ExecuteQuery->bindParam("i", "".($getSender) ."", "-1");
//or
$getSender -> $getColumnVal("balance");
$getSender = $getSender - $amount;
$ExecuteQuery->bindParam("i", "".($getSender) ."", "-1");
//acct2num
$ExecuteQuery->bindParam("i", "".($amount) ."", "-1");
//acct3num
$ExecuteQuery->execute();
?>
This ended up working for me:
while (!$getRecipients->atEnd())
{
$ExecuteQuery = new WA_MySQLi_Query($numeroseti);
$ExecuteQuery->Statement = "INSERT INTO transactions
(fromAcct, amount, fromAcctNewBalance, toAcctNewBalance, toAcct, description, timestamp)
VALUES (1, ?, ?, ?, ?, 'issuance', $time)";
$ExecuteQuery->bindParam("i", "".($amount) ."", "0");
$ExecuteQuery->bindParam("i", "".($startingbalance) ."", "-1");
$ExecuteQuery->bindParam("i", "".($amount) ."", "-1");
$ExecuteQuery->bindParam("i", "".($getRecipients->getColumnVal("acct_number")) ."", "-1");
$ExecuteQuery->execute();
$startingbalance = $startingbalance - $amount;
$getRecipients->moveNext();
}
$getRecipients->moveFirst();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.