-1
I’m trying to make a calculation before sending to the database:
<?php
session_start();
require_once 'config/init.php';
require 'config/check.php';
?>
<?php
require 'config/conexao.php';
try {
$PDO = new PDO( 'mysql:host=' . MYSQL_HOST . ';dbname=' . MYSQL_DB_NAME, MYSQL_USER, MYSQL_PASSWORD, array( PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' ) );
} catch ( PDOException $e ) {
echo 'Erro ao conectar com o MySQL: ' . $e->getMessage();
}
$descricao = $_POST ['descricao'];
$fornecedor = $_POST ['fornecedor'];
$custo = $_POST ['custo'];
$margemlucro = $_POST ['margemlucro'];
$estoque = $_POST ['estoque'];
$custo1 = $_POST ['custo'];
$margemlucro1 = $_POST ['margemlucro'];
$precofinal = $custo1 + ($custo1 / 100 * $margemlucro1);
$sql = "INSERT INTO produtosservicos (descricao, fornecedor, custo, margemlucro, precofinal, estoque) VALUES
(:descricao, :fornecedor, :custo, :margemlucro, :precofinal, :estoque)";
$stmt = $PDO->prepare($sql);
$stmt->bindParam(':descricao', $_POST['descricao'], PDO::PARAM_STR);
$stmt->bindParam(':fornecedor', $_POST['fornecedor'], PDO::PARAM_STR);
$stmt->bindParam(':custo', $_POST['custo'], PDO::PARAM_STR);
$stmt->bindParam(':margemlucro', $_POST['margemlucro'], PDO::PARAM_STR);
$stmt->bindParam(':precofinal', $precofinal);
$stmt->bindParam(':estoque', $_POST['estoque'], PDO::PARAM_STR);
$stmt->execute($stmt);
echo "<script>alert('Cadastro efetuado com sucesso!'); window.top.location.href = 'produtos-servicos.php';</script>";
?>
The calculation occurs, but does not save in the bank and does not give error. What can I be doing wrong? If I take the field of calculation, it saves normally. I changed it in the bank to int
, decimal
and finally I left just like varchar
.
Update: I found the error [28-Jan-2019 18:03:11 UTC] PHP Warning: PDOStatement::execute() expects parameter 1 to be array, object given in ******* on line 39
. I haven’t solved the problem yet.
$stmt->execute()
returnedtrue
?– Woss
expects parameter 1 to be array, object given in...
the code was sending to automatic index, had not seen the error.– Hebert Richard Masseno Dias
From what I see, you’re passing everything as a string in the bank, this is a value that doesn’t even need to be stored, it can be calculated by the bank in return
– bruno101
I thought about giving the change option if necessary. That’s why I’m storing everything. What you recommend, @bruno101 ?
– Hebert Richard Masseno Dias
Some people take pleasure in downvoting, I don’t understand...
– Hebert Richard Masseno Dias
They were probably negative because their question is unclear. You found the error, but have not yet added to the question. You even put the message in the comments, but omitted the most important parts: file name and error line. Could [Edit] the question and add all the details you have of the problem so far?
– Woss
Done, @Andersoncarloswoss.
– Hebert Richard Masseno Dias
And again you omitted error information as I just commented. Are you sure you put in the question exactly the snippet that has the error? I ask because the error clearly is complaining that you passed an object as parameter to
PDOStatement::execute()
, but in the passage you put in the question neither parameter has in this call.– Woss
I edited the question again, trying not to omit any kind of information, I ask for kindness to evaluate. Grateful.
– Hebert Richard Masseno Dias