1
My table itens_venda performs two calculations within a PHP code:
- The first calculation multiplies the price with the quantity of the product in the table
itens_venda(That part is OK). - The 2nd calculation updates the quantity of products available in the table
produto(That part is not OK).
The 1st calculation works perfectly because it affects the table itself, only that the 2nd calculation I do not know how to affect the other table within the PHP code.
That is, make the column quantidade table itens_venda is subtracted by the column quantidade table produto.
Ex: 20(quantidade da tabela produto) - 2(quantidade da tabela itens_venda) = 18 (quantidade da tabela produto atualizada);
Obs: I don’t want the use of Rigger, because it’s less complicated for me.
Tables and their columns
PHP code
<?php
require_once 'conexao.php';
if (isset($_POST['Inserir'])) {
$cd_produto = $_POST['cd_produto'];
$cd_funcionario = $_POST['cd_funcionario'];
$cd_cliente = $_POST['cd_cliente'];
$tipo_pagamento = $_POST['tipo_pagamento'];
$valor_item = $_POST['valor_item'];
$quantidade = $_POST['quantidade'];
$valor_total = ($valor_item * $quantidade); // 1º cálculo funciona perfeitamente
$atualiza_quantidade = ($quantidade - $quantidade); // 2º cálculo que não sei como faze-lo funcionar
try {
$insercao = "INSERT INTO itens_venda (cd_produto,cd_funcionario,cd_cliente,
tipo_pagamento,valor_item,quantidade,valor_total,data_venda)
VALUES (:cd_produto,:cd_funcionario,:cd_cliente,
:tipo_pagamento,:valor_item,:quantidade,:valor_total,CURRENT_TIMESTAMP())";
$insere_dados = $conexao->prepare($insercao);
$insere_dados->bindValue(':cd_produto',$cd_produto);
$insere_dados->bindValue(':cd_funcionario',$cd_funcionario);
$insere_dados->bindValue(':cd_cliente',$cd_cliente);
$insere_dados->bindValue(':tipo_pagamento',$tipo_pagamento);
$insere_dados->bindValue(':valor_item',$valor_item);
$insere_dados->bindValue(':quantidade',$quantidade);
$insere_dados->bindValue(':valor_total',$valor_total);
$insere_dados->execute();
} catch (PDOException $falha_insercao) {
echo "A insercão não foi feita".$falha_insercao->getMessage();
}
}
?>

Example: Two products sold were registered in the column
quantidadetableitens_vendawhich will be subtracted by the columnquantidadetableprodutowhich has 20 products, that is to say20 - 2 = 18.– user198162
Search by Trigger. https://dev.mysql.com/doc/refman/8.0/en/triggers.html
– anonimo
@anonimo I’m wanting to do this without help of triggers.
– user198162
@anonimo I had many problems and lost hours with the triggers, so the way they recommended me about doing these operations within PHP were welcomed.
– user198162