简体   繁体   English

在php中处理数字输入失败

[英]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. 我有一个输入框,告诉我的用户将其Google Plus地址复制并粘贴到其中。 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: 这是处理输入的PHP代码:

$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. 当用户输入最多10位数字时,它会很好地存储数字。 When the user enters over 10 digits, it stores this number in the database: "2147483647". 当用户输入10位以上的数字时,它将此数字存储在数据库中:“ 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! 我已经在我的代码中搜索了“ 2147483647”,但没有任何代码!

http://dev.mysql.com/doc/refman/5.5/en/integer-types.html : 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. 请改用(VAR)CHAR。

I think your database field is not correct. 我认为您的数据库字段不正确。 Change it to bigint or float (or varchar...). 将其更改为bigint或float(或varchar ...)。

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

agree with cbroe above that varchar is probably best choice 同意cbroe的观点,varchar可能是最佳选择

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. 您必须在数据库中检入表,创建数据库时,必须为选项卡中的每个字段选择一种类型,例如对于整数字段,max_value为2,147,483,647,因此,如果需要更多空间,可以使用bigint或使用此数字仅像数字序列一样,您也可以使用字符串。

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 您应使用BIGINT unsigned存储大于2147483647或小于-2147483647的数字,因为google plus ID是类似于116686373347867906934的数字

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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