这就是数据库的工作方式
您似乎很可能正在使用 MySQL,而您所描述的只是它的工作原理:
mysql> select * from posts where id = 1;
+----+-----------+------------------------+---------------------+----------+
| id | title | body | created | modified |
+----+-----------+------------------------+---------------------+----------+
| 1 | The title | This is the post body. | 2013-08-01 07:34:57 | NULL |
+----+-----------+------------------------+---------------------+----------+
1 row in set (0.00 sec)
mysql> select * from posts where id = "1and this text";
+----+-----------+------------------------+---------------------+----------+
| id | title | body | created | modified |
+----+-----------+------------------------+---------------------+----------+
| 1 | The title | This is the post body. | 2013-08-01 07:34:57 | NULL |
+----+-----------+------------------------+---------------------+----------+
1 row in set, 1 warning (0.00 sec)
通过这样的输入,数据库将在执行查询之前将该值转换为整数。
如果您想阻止您的应用程序将这两个用户输入视为相同 - 您需要在使用之前验证用户输入并确保它是数字的。