0

I have created a form that contains fields used to accept a floating value as input. My goal is to be able to store these values in a DB and use them later on. Everything I've tried so far has resulted in failure.

I have tried changing the input field type from text to number, float, and even number with a small step size. It seems that when I request the value of the field with php and then send it to my DB, something goes wrong and stores only the nearest whole number. Ideas?

My form code is:

<form name ="chemAdd"  action="submitChemData.php" method="POST" id="chemData">

                <fieldset>
                <legend>Chemical Information</legend>
                <label for="chemName">Chemical Name:</label>
                <input type="text" name="chemName" />

                <label for="chemFormula">Molecular Formula:</label>
                <input type="text" name="chemFormula" />
                </fieldset>
                <br />

                <fieldset>
                <legend>Antoine Constants</legend>
                <label for="A">A:</label>
                <input type="number" name="A" size="10" min="0" max="9999" step="0.00000001" />

                <label for="B">B:</label>
                <input type="float" name="B" size="10"/>

                <label for="C">C:</label>
                <input type="text" name="C" size="10"/>

                <br /><br />

                <label for="unitT">Unit (Temperature):</label>
                    <select name="unitT" >
                        <option value="K">Kelvin</option>
                        <option value="C">Celcius</option>
                        <option value="F">Fahrenheit</option>
                    </select>

                <label for="unitP">Unit (Pressure):</label>
                    <select name="unitP" >
                        <option value="P">Pascal</option>
                        <option value="Hg">mm Hg</option>
                        <option value="bar">bar</option>
                    </select>

                </fieldset>
                <br />
                <fieldset>
                <legend>Reference Information</legend>
                <label for="Tmin">Temp. Minimum:</label>
                <input type="float" name="Tmin" size="10"/>

                <label for="Tmax">Temp. Maximum:</label>
                <input type="float" name="Tmax" size="10"/>
                <br /><br />
                <label for="Reference">Reference:</label>
                <input type="text" name="Reference" size="60"/>

                </fieldset>
                <br />
                <input type="submit"  value="Add to Database" id="submit" />    

                </form>
            </form>

My PHP code is:

  require_once 'dbConnectChem.php';


$name = trim(strtoupper($_REQUEST['chemName']));  
$formula= trim(strtoupper($_REQUEST['chemFormula']));  
$A =  $_REQUEST['A']; 
$B = $_REQUEST['B']; 
$C = $_REQUEST['C']; 
$unitT = $_REQUEST['unitT']; 
$unitP = $_REQUEST['unitP']; 
$Tmin = $_REQUEST['Tmin']; 
$Tmax = $_REQUEST['Tmax']; 
$Reference = $_REQUEST['Reference']; 


//INSERT Query
mysql_query("INSERT INTO Antoine (Name,Formula,A,B,C,unit_T,unit_P,Tmin,Tmax,Reference) VALUES ('{$name}','{$formula}','{$A}','{$B}','{$C}','{$unitT}','{$unitP}','{$Tmin}','{$Tmax}','{$Reference}') ")
    or die(mysql_error());
4

1 回答 1

1

Use DECIMAL data type instead of Float. AS described in this link, sometimes floating point values leads to issues, while Decimal works fine.

Use it like this way :

CREATE TABLE t1 (i INT, d1 DECIMAL(9,2), d2 DECIMAL(9,2));

Here 9 is the precision and 2 is the scale.Change it accordingly

于 2012-06-20T19:38:39.767 回答