0

I need help to write information in the Mysql DB, using PHP. I have a string that stores any value, for example 1.279,35, and I need to convert it to float or decimal (10.2). I tried to use the PHP function "str_replace" to convert the comma to a dot, but it is not working. Thanks.

<?php

include_once "conexao.php";

$dados = filter_input_array(INPUT_POST, FILTER_DEFAULT);

if (empty($dados['descricao'])) {
    $retorna = ['erro' => true, 'msg' => "<div class='alert alert-danger' role='alert'>Erro, é necessário preencher o campo descricao.</div>"];
} elseif (empty($dados['valor'])) {
    $retorna = ['erro' => true, 'msg' => "<div class='alert alert-danger' role='alert'>Erro, é necessário preencher o campo valor.</div>"];
} else {
    $valtemp = $dados['valor']);
    $valtemp = str_replace(".", "", $valtemp);
    $valtemp = str_replace(",", ".", $valtemp);
    $dados['valor']) = $valtemp;
    $query_peca = "INSERT INTO pecas 
                            (descricao, modelo, nserie, fornecedor, 
                            datacompra, valor, condicao, observacao) 
                    VALUES (:descricao, :modelo, :nserie, :fornecedor, 
                            :datacompra, :valor, :condicao, :observacao)";
    $cad_peca = $conn->prepare($query_peca);
    $cad_peca->bindParam(':descricao', $dados['descricao']);
    $cad_peca->bindParam(':modelo', $dados['modelo']);
    $cad_peca->bindParam(':nserie', $dados['nserie']);
    $cad_peca->bindParam(':fornecedor', $dados['fornecedor']);
    $cad_peca->bindParam(':datacompra', $dados['datacompra']);
    $cad_peca->bindParam(':valor', $dados['valor']);    
    $cad_peca->bindParam(':condicao', $dados['condicao']);
    $cad_peca->bindParam(':observacao', $dados['observacao']);
    $cad_peca->execute();   

    if ($cad_peca->rowCount()) {
        $retorna = ['erro' => false, 'msg' => "<div class='alert alert-success' role='alert'>Peça cadastrada com sucesso.</div>"];
    } else {
        $retorna = ['erro' => true, 'msg' => "<div class='alert alert-danger' role='alert'>Erro: peça não cadastrada.</div>"];
    }
}

echo json_encode($retorna);
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • 1
    [It should work](https://3v4l.org/ANQIS). What problems are you having with it? Though `$valtemp = $dados['valor']);` and `$dados['valor']) = $valtemp;` are typos. – aynber Nov 03 '22 at 18:25
  • It might be a good idea to show us the schema for this table. Do a `SHOW CREATE TABLE pecas;` in phpMyAdmin or similiar or the mysql command line and copy/paste the output to your question – RiggsFolly Nov 03 '22 at 18:30
  • Also, is anything written to the database? If so, what is written to the database – RiggsFolly Nov 03 '22 at 18:32
  • I'd strongly recommend using php's native parsing; https://www.php.net/manual/en/numberformatter.parse.php – MatBailie Nov 03 '22 at 18:32
  • Possible **TYPO** What is `$valtemp = $dados['valor']);` and `$dados['valor']) = $valtemp;` Do you have basic Error reporting turned on? – RiggsFolly Nov 03 '22 at 18:34
  • Add [error reporting](http://stackoverflow.com/questions/845021/) to the top of your file(s) _while testing_ right after your opening PHP tag for example. Even if you are developing on a server configured as LIVE you will now see any errors. ` – RiggsFolly Nov 03 '22 at 18:35

1 Answers1

-1

I had faced similar issue while saving the currency value in the DB and have used floatval() as shown below.

$valtemp = "1.279,35";
echo floatval(str_replace(',', '.', str_replace('.', '', $valtemp)));