简体   繁体   English

如何将计算字段插入数据库?

[英]How to insert calculated field into database?

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.该字段来自我遇到问题的fromAcctNewBalance 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.例如,如果fromAcctNewBalance起始余额为 600,发给用户的金额为 10,则添加的下一条记录的fromAcctNewBalance应为 590,依此类推,直到它用完要添加到表中的记录为止。

Does anyone have any ideas on how I can fix this issue?有人对我如何解决此问题有任何想法吗?

I think the mistakes lies in the use of -> inside the bindParam .我认为错误在于在bindParam中使用->

So I tried to do the substraction without the bindParam and $getSender->$getColumnVal - $amount;所以我尝试在没有bindParam$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.我不清楚您的$getColumnVal("balance")是方法还是实例。 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? 我们在哪里使用 PHP 中的 object 运算符“->”?

https://www.php.net/manual/de/language.types.object.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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM