Insert decimals from php to phpMyadmin

1

how are you? I have a question, I want to enter a value from php, such as 155.60, but when I add it to the phpMyadmin database, it saves it as 155, that is, it rounds it, but I need it if or if I presicion it. Place FLOAT on the BD, also probe with REAL and DOUBLE. But it does the same. This is my code from php:

    $sql="insert into productos (idProducto,Nombre_Producto, precio, Detalles, id_Tipo_Moneda,
                                    id_Categoria, Stock, idEstadoEliminacion, CantidadAlarma )values(:codProd,:nombre, :pre, :det, :tmoneda,
                                    :cat, :stock, :estelim, :Alarma)";

                                    $sentencia = $con->prepare($sql);
                                    $sentencia-> bindParam(':codProd', $codProd, PDO::PARAM_STR);
                                    $sentencia-> bindParam(':nombre', $nom, PDO::PARAM_STR);
                                    $sentencia-> bindParam(':pre', $pre, PDO::PARAM_INT);
                                    $sentencia-> bindParam(':det', $det, PDO::PARAM_STR);
                                    $sentencia-> bindParam(':tmoneda', $tMoneda, PDO::PARAM_STR);
                                    $sentencia-> bindParam(':cat', $cat, PDO::PARAM_INT);
                                    $sentencia-> bindParam(':stock', $stock, PDO::PARAM_INT);
                                    $sentencia-> bindParam(':estelim', $estel, PDO::PARAM_INT);
                                    $sentencia-> bindParam(':Alarma', $Alarma, PDO::PARAM_INT);

Could someone help me?

    
asked by GALS 18.11.2018 в 05:33
source

1 answer

1

To save data with decimals with greater precision; you must use the data type DECIMAL() which will request the following:

DECIMAL(A, B);
  • A is the maximum number of spaces the column will have
  • B is the number taken to make the presicion
  • The numerical data that require greater precision as they are: prices or monetary units of banks

    EXPLANATION

    If for example your quantities were always in the following range:

    desde 100.00
    hasta 999.99
    

    Then the declaration of your column should be:

    precio DECIMAL(5, 2)
    

    Where we are indicating that there will be a total of 5 digits for the values entered in the price column, but that 2 of them will be used to store the decimal numbers of the record of each precio

    clarification

    Within the structure of your PHP code, you have a column to fill out called precio that you then treat in this way:

    $sentencia-> bindParam(':pre', $pre, PDO::PARAM_INT);
    

    The above is incorrect, because if you are waiting for a price with decimals, the data type INT does not help us then you should change it to

    $sentencia-> bindParam(':pre', $pre, PDO::PARAM_STR);
    

    Why would it be the closest approximation to deal with a data type with decimals

        
    answered by 18.11.2018 / 05:40
    source