0
Hello, I’m a beginner in PHP and Mysql. I would like to have a form where I insert a value (in real, in case), disappear with what I have already saved in the database, and finally display to the user the result of this calculation.
It’s something very simple, just for study.
I have a form and a table in the database with a DECIMAL field. I can already save in the database, but if I type in the input a value of type "1.280,90", in my database it saves only as "1":
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jJulius</title>
</head>
<body>
<?php
$servername = "localhost";
$database = "jjulius";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
<?php
if(isset($_POST['save']))
{
$valor=$_POST['valor'];
$sql = "INSERT INTO valor (valor) VALUES ";
$sql .= "('$valor')";
mysqli_query($conn,$sql) or die("Erro ao tentar cadastrar registro");
mysqli_close($conn);
}
?>
<form action="" method="post">
<input type="text" name="valor" />
<button type="submit" name="save">Enviar</button>
</form>
</body>
</html>
Can someone help me how to save and display this data properly, please?
Thanks in advance. :)
1.280,90 is not a decimal format for the database.
– user60252
The correct format for the value 1.280,90, should be 1280.90, standard Mysql decimal number format. you could make a tractive to get this format using $value = "1.280,90"; $value = REPLACE( REPLACE( $value, '.' ,'' ), ',', '.' ). and when to display on screen do the tracing to display the structure again.
– Marcus Italo