-1

I'm trying to insert user information into the database. I am able to add information, The only problem that I am facing is that, The mob_number as well as pmob_number gets stores as 2147483647 in the database server no matter what 10 digit number I give in the form. I know that this is pretty basic, I'm just starting out. Thanks in Advance :-)

<?php
$servername = "localhost";
$username   = "root";
$password   = "";
$conn = mysqli_connect($servername, $username, $password) or die("unable to connect to host");
$sql = mysqli_select_db($conn, 'vcet') or die("unable to connect to database");
//code to insert into db...
$user_name    = $_POST['user_name'];
$roll_number  = $_POST['roll_number'];
$user_batch   = $_POST['user_batch'];
$user_from    = $_POST['user_from'];
$user_year    = $_POST['user_year'];
$user_level   = $_POST['user_level'];
$email_id     = $_POST['email_id'];
$F_name       = $_POST['F_name'];
$m_name       = $_POST['m_name'];
$g_name       = $_POST['g_name'];
$mob_number   = $_POST['mob_number'];
$addressline1 = $_POST['addressline1'];
$addressline2 = $_POST['addressline2'];
$city         = $_POST['city'];
$pmob_number  = $_POST['pmob_number'];
$land_number  = $_POST['land_number'];
$cutoff       = $_POST['cutoff'];
$dept         = $_POST['dept'];
$medium       = $_POST['medium'];
$locality     = $_POST['locality'];
$intrest      = $_POST['intrest'];
$scholar      = $_POST['scholar'];
$income       = $_POST['income'];
$user_caste   = $_POST['user_caste'];
$user_admit   = $_POST['user_admit'];
$user_stay    = $_POST['user_stay'];
if (!filter_var($email_id, FILTER_VALIDATE_EMAIL)) {
    echo "Invalid email format";
} else {
    //executes the rest of the code
    if (!preg_match('/^\d{10}$/', $pmob_number)) // phone number is valid
        {
        echo "Phone number invalid !";
    } else // phone number is  valid
        {
        $msg        = "";
        $num_length = strlen((string) $pmob_number);
        if ($num_length != 10) {
            echo "Enter a valid number";
        } else {
            $user_info = "INSERT INTO studentinfo" . "     (user_name,roll_number,user_batch,user_from,user_year,user_level,email_id,F_name,m_name,g_name,mob_number,addressline1,addressline2,city,pmob_number,land_number,cutoff,dept,medium,locality,intrest,scholar,income,user_caste,user_admit, user_stay)" . "VALUES ('$user_name', '$roll_number','$user_batch','$user_from','$user_year','$user_level','$email_id','$F_name','$m_name','$g_name','" . $mob_number . "','$addressline1','$addressline2','$city','$pmob_number','$land_number','$cutoff','$dept','$medium','$locality','$intrest','$scholar','$income','$user_caste','$user_admit','$user_stay')";
            if (!mysqli_query($conn, $user_info)) {
                die('Error: ' . mysqli_error($conn));
            }
            echo "Your information was added to the database.";
            mysqli_close($conn);
        }
    }
}
?> 
```



Sasuke Uchiha
  • 421
  • 6
  • 17
  • 2
    It would seem that the type of the "number" is a 4-byte integer somewhere along the way, instead of a string. – Gordon Linoff Jul 30 '16 at 16:46
  • show a clear sample – ScaisEdge Jul 30 '16 at 16:47
  • 1
    Some sensible code indentation would be a good idea. It help us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Jul 30 '16 at 16:49
  • Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared statement and parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Jul 30 '16 at 16:50
  • I hope that's not a real phone number?!? – openwonk Jul 30 '16 at 16:56

1 Answers1

5

I`m trying to insert mobile number into database but instead of the mobile number it is saved as 2147483647

It looks like you got your database schema wrong. Ensure column you put your phone number data into is rather of string type (i.e. VARCHAR), and not numeric (not INTEGER, BIGINT, DECIMAL etc)

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141