1

I'm having a little code, where the user can select a number with or without decimal places. I added the step attribute to my input type="number", So the user can insert decimals, as shown:

    <form method="post" >
    <input type='number' name='price' min='1' max='30' step=".01" required> 
    <input type='submit' name='submit'> 
    </form>
if(isset($_POST['submit']))
{
    try{
            require("conecction.php");
            $db->beginTransaction();
            $ins = $db->prepare("INSERT INTO items (price) VALUES (?)");
            $ins->bindParam(1,$_POST['price']);
            $ins->execute();
    }
    catch(PDOException $ex)
    {
      $db->rollBack();
      echo "<p style=color:red;> There was an connection error.<p>";
      die($ex->getMessage());
    }
}

and my SQL table has the following format:

CREATE TABLE `items` (
  `ID` int(11) NOT NULL,
  `price` decimal(6,2) NOT NULL,
) 

This code is functional and inserts the data into the DB as expected to be, but only it is inserting the closest number to the decimal point, for example the user inserts 21.4 I get 21 in my DB.

droopsnoot
  • 931
  • 1
  • 7
  • 11
Ahmed Kena
  • 27
  • 6

1 Answers1

-2

Change the data type of the field to FLOAT As you assign the float datatype in DB then you will be able to save integer as well as float decimal values.

http://prntscr.com/1uvulon

Atmiya Kolsawala
  • 475
  • 4
  • 12