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:
Here as the table structure:
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:
As it stands, testing with the value 3,803.20 o echo $valor_total
returns the value: 3.803
Felipe, I made the change and it kept coming wrong. I have to change the structure of BD tbm ?
– Leo
Could post how it was saved in DB?
– Felipe F. de Paula
Switch from Float to Double, so you can store a higher value.
– Felipe F. de Paula
I switched to Double and it still didn’t work. I will edit the question with the new print.
– Leo
**Double Did you dump the $valor_total variable? Post it to me
– Felipe F. de Paula
I guessed the test I performed
– Felipe F. de Paula
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
– Leo
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
– Leo
From what I understand the problem is in your code try to adapt my test in your code.
– Felipe F. de Paula
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.
– Leo
In my test change this: $value = "R$3,084.05"; For this $value = $_POST['total_request']; And dump the dump
– Felipe F. de Paula
I switched and returned: C: wamp64 www project_vale_vip request.php:32:float 0 0
– Leo
It seems that the value of $_POST['total_request'] is 0 makes a dump of it.
– Felipe F. de Paula
If I put a dump, it doesn’t even show up on the screen.
– Leo
He should show up, if he doesn’t show up!
– Felipe F. de Paula
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.
– Leo