Sum at time of INSERT with php

Asked

Viewed 140 times

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.

1 answer

1


Whoa, all right? Come on!

You said:

I need to add these values and play them in the variable Valordebito and perform the INSERT grouped

If you need to add up the values to only then perform the INSERT, the mounting of your code is wrong. The way you have mounted will be made several recordings with incremental values and not grouped.

To do with grouped values, first do the sum in a variable and write after the foreach.

For an array with treated values: $SomaValorDebito = array_sum ( $ValorDebitado );

In case you need any treatment:

$ValorDebitado = ['0,73', '0,75', '0,85', '1,25', '8,29'];

//SETA UM VALOR PADRÃO PARA A VARIÁVEL, SENÃO DÁ PAU
$SomaValorDebito = 0;

foreach($ValorDebitado as $key => $Soma) {

    //Colocar os valores em formato americano para realizar a soma corretamente
    $SomaValorDebito += str_replace(',', '.', $Soma);

    // NÃO GRAVA NADA AQUI
}

// 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();  

Hugs!

  • Ual, thanks @Tiago Sabatini, helped too much, thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.