简体   繁体   中英

Processing numeric input in php fails

I have an input box that tells my users to copy and paste their Google Plus address into it. This is the code for the input box:

<div class="row-fluid"><div class="span3 sidebar"><h4>Google Plus:</h4></div>
<div class="span9"><p> <input type="text" name="gplus" value="$data->googleplus" placeholder="Google Plus Profile URL">
</p></div></div><br/>

And this is the PHP code that processes the input:

$gplus=preg_replace("/[^0-9]/", "", $_POST['gplus']);
mysql_query( "UPDATE contacts SET googleplus='$gplus' WHERE username='$user'" )
or die ( "Could not update GooglePlus" );

I only need the numbers at the end of their address, as the rest is the same for everyone. Here is the problem. When the user enters up to 10 digits, it stores the numbers just fine. When the user enters over 10 digits, it stores this number in the database: "2147483647". Always the same number. Any letters are stripped just as they should be, but any more than ten digits in there causes that number to come up.

I have searched my code for "2147483647" and that is not in any of it!

http://dev.mysql.com/doc/refman/5.5/en/integer-types.html :

Type: INT
Maximum Value: 2147483647

You are obviously storing your values as integers, and when the number is too large, you get an overflow, and end up with the max value.

You should not store these values as integers anyway, since they aren't numbers – they have no numerical meaning, they are just an identifier. Use a (VAR)CHAR instead.

I think your database field is not correct. Change it to bigint or float (or varchar...).

adding link: http://dev.mysql.com/doc/refman/5.5/en/integer-types.html

agree with cbroe above that varchar is probably best choice

edited to agree with better answer :)

You have to check in your table inside the database, when you create a database you chose a type for each field in your tab, for example for integer field the max_value is 2,147,483,647 so if you need more space you can use bigint or if you use this number only like a sequence of numbers you can use also string.

i hope to will be usefull for you, bie (and sry for my english!)

You should use BIGINT unsigned to store a number great than 2147483647 or less than -2147483647 since the google plus ID is a number like 116686373347867906934

Read this part http://dev.mysql.com/doc/refman/5.5/en/out-of-range-and-overflow.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM