Bugando Bank Field with Thousand values

Asked

Viewed 39 times

2

Guys, I know it’s a very basic question, but how do I make a field of my bank accept values where these are values like 1000+ and also come as decimals. I will show an example below in the value of: 3.084,05, the shape that this fell in the BD field: inserir a descrição da imagem aqui

Here as the table structure:

inserir a descrição da imagem aqui

And here the PHP code that inserts the Database:

<?php

<?php

// Submit do arquivo index.php
error_reporting(E_ERROR);
header('Content-Type: text/html; charset=utf-8');
include 'conexao.php';

# Inclusão do Pedido
$id_pedido = $_POST['id_pedido'];
$id_fornecedor = $_POST['fornecedor_id'];
$nome_fornecedor = $_POST['select_fornecedor'];
$nome_input_fornecedor = $_POST['fornecedor_new_input'];
$cnpj = str_replace(".", "", str_replace("/", "", str_replace("-", "", $_POST['cnpj'])));
$valor_total = floatval(substr(str_replace("R$", "", str_replace(",", ".", $_POST['total_pedido'])),2));
$loja = $_POST['select_loja'];

echo $valor_total;
die();


if (isset($_POST['change_fornecedor'])) {
    $sql = "INSERT INTO pedido VALUES (NULL, '{$id_fornecedor}', '{$nome_input_fornecedor}','{$cnpj}', NOW(), {$valor_total}, '{$loja}')";
    if (!$connect->query($sql) === true) {
        die("Erro na inserção de pedido: " . $sql . "<br>" . $connect->error);
    }
} else {
    $sql = "INSERT INTO pedido VALUES (NULL, '{$id_fornecedor}', '{$nome_fornecedor}', '{$cnpj}', NOW(), {$valor_total}, '{$loja}')";
    if (!$connect->query($sql) === true) {
        die("Erro na inserção de pedido: " . $sql . "<br>" . $connect->error);
    }
}

As requested, a post-code and structure change print: inserir a descrição da imagem aqui

As it stands, testing with the value 3,803.20 o echo $valor_total returns the value: 3.803

1 answer

1

In the value field you have to:

  • Remove "R$"
  • Remove "."
  • Exchange "," for "."

    $value_total = (float) str_replace(",", "." , str_replace(["R$", ", "." ], "", $_POST['total_request']));

**You are using a substr function that is returning part of the value. And there is no need in this case.

I performed the test below:

$valor = "R$ 3.084,05";
$valor_total = (float) str_replace(",", ".", str_replace(["R$", " ", "."], "", $valor)); 
echo $valor_total;
  • Felipe, I made the change and it kept coming wrong. I have to change the structure of BD tbm ?

  • Could post how it was saved in DB?

  • Switch from Float to Double, so you can store a higher value.

  • I switched to Double and it still didn’t work. I will edit the question with the new print.

  • **Double Did you dump the $valor_total variable? Post it to me

  • I guessed the test I performed

  • I put a var_dump on the total order and the total price of the items. The return was this: C: wamp64 www project_vale_vip request.php:17:float 0 C: wamp64 www project_vale_vip request.php:48:null

  • With your test you got the following: C: wamp64 www project_vale_vip request.php:17:float 0 C: wamp64 www project_vale_vip request.php:48:null 3084.05

  • From what I understand the problem is in your code try to adapt my test in your code.

  • I put a die() at the end of the code to not scroll the INSERT and your test was before this. Values coming from var_dump are always the same, regardless of the order value / items.

  • In my test change this: $value = "R$3,084.05"; For this $value = $_POST['total_request']; And dump the dump

  • I switched and returned: C: wamp64 www project_vale_vip request.php:32:float 0 0

  • It seems that the value of $_POST['total_request'] is 0 makes a dump of it.

  • If I put a dump, it doesn’t even show up on the screen.

  • He should show up, if he doesn’t show up!

  • Felipe, I edited the question showing the value appearing now. With the code you passed, it doesn’t really appear, but leaving in the "original" way, it is returning the value only without the decimals. Even so, in BD it does not even reach this value returned.

Show 11 more comments

Browser other questions tagged

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