0
I’m trying to make a sum at the time of a INSERT in a table but I’m having difficulties to accomplish, in my case, the sum needs to take into account a grouping by phone number and user, for example:
Telephone 99858-8888 user’s ID 77 had 5 calls with their values, being:
0,73
0,75
0,85
1,25
8,29
I need to add these values and play them in the variable ValorDebito and carry out the INSERT grouped.
The structure of my table is like this:
`IdItemProcesso` INT(11) NOT NULL AUTO_INCREMENT COMMENT 'Identificador único do item do processo',
`IdProcesso` INT(11) NOT NULL COMMENT 'Código do processo',
`IdUsuario` INT(6) NOT NULL COMMENT 'Código do usuário',
`NumeroTelefone` VARCHAR(25) NOT NULL COMMENT 'Número do telefone',
`ValorDebitado` DECIMAL(6,2) NOT NULL COMMENT 'Valor debitado para o usuário',
`Mes` INT(2) NOT NULL COMMENT 'Mês do processo',
`Ano` INT(4) NOT NULL COMMENT 'Ano do processo',
I tried something, it was like this:
foreach($ValorDebitado as $key=>$Soma) {
$SomaValorDebito+= $Soma;
// INSERINDO DO PROCESSO NA TABELA cpdItemProcesso
$crud = $conexao->prepare("INSERT INTO cpdItemProcesso (IdProcesso,IdUsuario,NumeroTelefone,ValorDebitado,Mes,Ano) VALUES (?,?,?,?,?,?)");
$crud->bindParam(1, $IdProcesso, PDO::PARAM_INT);
$crud->bindParam(2, $IdUsuario, PDO::PARAM_INT);
$crud->bindParam(3, $NumeroTelefone, PDO::PARAM_STR);
$crud->bindParam(4, $SomaValorDebito, PDO::PARAM_STR);
$crud->bindParam(5, $Mes, PDO::PARAM_INT);
$crud->bindParam(6, $Ano, PDO::PARAM_INT);
$crud->execute();
}
If it is a calculated field wouldn’t it be more practical to create a view? If you really want to keep such a sum in the bank create a Trigger.
– anonimo