Search code examples
phpmysqldatabaseint

PHP MySQL insert integer with thousand seperator


I have a PHP code that will insert integer numbers with thousand separator into the database. However, it could not save the whole integer.. Exp: 10,234,234 <-- Will only appear as 10 in the database. 5,434 <- Appear as 5

My code:

 <html>
<body>

<?php
session_start();
$date =  $_SESSION['storeDate'];
$a = $_SESSION['storeA'];
$l = $_SESSION['storeL'];
$p = $_SESSION['storeP'];
$m = $_SESSION['storeM'];

$con = mysql_connect("localhost","root","password");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

    mysql_select_db("productno", $con);

    $sql="INSERT INTO records (Date, A, L, P, M)
    VALUES
    ('$date', '$a', '$l', '$p', '$m')"; 

    if (!mysql_query($sql,$con))
    {
        echo ("<SCRIPT LANGUAGE='JavaScript'>
        window.alert('Save Failed.')
        window.location.href='main.php'
        </SCRIPT>");
    }else{
       echo ("<SCRIPT LANGUAGE='JavaScript'>
        window.alert('Record Added.')
        window.location.href='main.php'
        </SCRIPT>");
 }

?>

</body>
</html>

How do I modify it so that it will store the whole number -> 10,234,234 Thanks


Solution

  • If the data type in your column is INT, then no matter what you try, it will always be truncated.

    If you really want that figure to be inserted, alternatively, you could do it like this:

    $number = '10,234,234';
    $number = filter_var($number, FILTER_SANITIZE_NUMBER_INT); // becomes 10234234
    // insert code the rest of the way
    

    Sidenote: By the way, try to migrate to the improved extension which is mysqli_* or PDO and utilize prepared statements.