Insert, add and display value (in real) using PHP and Mysql

Asked

Viewed 2,464 times

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

    1.280,90 is not a decimal format for the database.

  • 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.

1 answer

0


First, the field DECIMAL Mysql requires exact accuracy, by my option I use FLOAT.

Then the value that will be saved must contain a maximum of one . separating the decimals, 1234.56 for example, for this you need to keep only the numbers and replace the , for ..

The function preg_replace(), here, just keep the numbers and the comma:

$valor=preg_replace("/[^0-9,]+/i","",$_POST["valor"]);

The function str_replace(), here, replace the comma with the end point:

$valor=str_replace(",",".",$valor);

Executes the INSERT:

$sql = "INSERT INTO valor (valor) VALUES ('$valor')"; 
mysqli_query($conn,$sql) or die("Erro ao tentar cadastrar registro");

Executes the SELECT, using CONCAT to add the R$ the result, and COUNT to add up the values:

$sql = "SELECT CONCAT('R$ ', SUM(valor)) AS total FROM valor";

if($result = mysqli_query($conn,$sql)):
    while($row = mysqli_fetch_assoc($result)):
        echo "Total: ".$row["total"];
    endwhile;
    mysqli_free_result($result);
endif;
  • Perfect! I will put a mask on the form so that the number is saved in the correct way, thank you very much!

  • I thank you for helping, if I can mark the answer, I will thank you.

Browser other questions tagged

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