PHP percentage calculation and save to Database

Asked

Viewed 212 times

-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() returned true?

  • expects parameter 1 to be array, object given in... the code was sending to automatic index, had not seen the error.

  • 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

  • I thought about giving the change option if necessary. That’s why I’m storing everything. What you recommend, @bruno101 ?

  • Some people take pleasure in downvoting, I don’t understand...

  • 1

    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?

  • Done, @Andersoncarloswoss.

  • 2

    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.

  • I edited the question again, trying not to omit any kind of information, I ask for kindness to evaluate. Grateful.

Show 4 more comments

2 answers

0

$stmt->bindParam(':precofinal', $_POST['precofinal'], PDO::PARAM_STR);

You don’t have a $_POST['precofinal']. I advise to change the values of the bank, leaving everything as a string is not a good practice, this value, for example, could be calculated and returned by the bank itself and n~;ao by the application.

  • Do you have a reference for study, please? The $_POST really passed, I did not see, but even correcting, the error persists.

0


I recommend reading again the documentation of the functions you are using: execute, bindParam.

The method PDOStatement::execute() expects a parameter of type array, which is optional, but you pass as parameter $stmt. You basically called an object method and passed it as a parameter - it hardly makes sense.

As already assigned the values with bindParam, do not need to pass parameters to the execute:

$stmt->execute();

Reading the documentation you will see that the method has return. Do not ignore it.

  • Your information was useful and solved my problem. Finally, I would like to put the final code, for information. How should I proceed?

  • @Cyberplague No need, just signal how useful the answer using the

  • Thanks for the support. I only regret my lack of understanding in the development of the issue, I am sad by the downvote. I hope to settle these questions. Hug.

Browser other questions tagged

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