Mysql – PHP value won’t exceed 127

mysqlrangetypes

Here's part of the code on my form:

<br><input type="checkbox" checked="yes" name="country[]" value="1" />Asia/Pacific Region
<br><input type="checkbox" checked="yes" name="country[]" value="2" />Europe
<br><input type="checkbox" checked="yes" name="country[]" value="3" />Andorra
...
<br><input type="checkbox" checked="yes" name="country[]" value="250" />Jersey
<br><input type="checkbox" checked="yes" name="country[]" value="251" />Saint Barthelemy
<br><input type="checkbox" checked="yes" name="country[]" value="252" />Saint Martin

and this is my php code:

$country=$_POST['country'];
...
foreach ($country as $country) {
    $sql="INSERT INTO sites_countries (siteID, country) VALUES ('$id', '$country')";
    # execute SQL command
    if (!mysql_query($sql,$con)) {
        die('Error: ' . mysql_error());
    }
}

but I looked at my database and it seems to only get to 127, then $country is always 127

Best Solution

If the field "country" is defined as a signed tinyint the maximum value is 127.
see http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html

edit: even if your MySQL server allows you to insert data that is out-of-range you can get information about the truncation, e.g. with

SHOW WARNINGS
which would result in something like
"Level";"Code";"Message"
"Warning";"1264";"Out of range value for column 'i' at row 1"
You can also put the server in strict mode and get an error if any data is out-of-range.
see http://dev.mysql.com/doc/refman/5.0/en/server-sql-mode.html