【问题标题】:MYSQL accepts a string value with letters for an INT datatypeMYSQL 接受带有字母的字符串值作为 INT 数据类型
【发布时间】:2014-03-15 04:22:32
【问题描述】:

我正在使用 php 和 mysql。当用户单击提交按钮时,GET 查询字符串如下所示:

http://mywebsite.com/category/section?article=1

我这样查询:

"SELECT * FROM article WHERE art_id='$article'"

art_id 是一个 int(11) 数据类型,但如果我有正确的数字作为前缀,它会接受一个带有字母的值,例如:http://mywebsite.com/category/section?article=1asd。如果我传递所有字母 (?article=asd),它不会接受,但如果我传递带有字母 (?article=12asd) 的数字,它会接受并返回带有 art_id12 的文章。我怎样才能防止这种情况发生?

我已经在 MySQL 命令行客户端上直接尝试过,它接受带有字母的值。

【问题讨论】:

  • 只需去掉语句art_id='$article'中变量周围的单引号

标签: php mysql get


【解决方案1】:

您可以通过 if 条件检查您的代码:

You can check your GET variable 'article' before passing into mysql query to prevent the issue you are facing:
solution 1: if(is_numeric($_GET['article'])){your query}
solution 2: intval($_GET['article']);//cast the variable and then pass into query
solution 3: if(is_int($_GET['article'])){your query}

希望这些解决方案对您有用。

【讨论】:

    【解决方案2】:

    在您的表中将int(11) 更改为varchar(11),前提是它不是PK ..在此scnerio 中您可以包含可变字符

    【讨论】:

      【解决方案3】:

      解决办法是: if(isset($_GET['article']) && (is_numeric($_GET['article'])){你的查询} // 添加 isset 函数用于未设置的 viarables 和 is_numeric 函数数字验证。 祝你好运!

      【讨论】:

        猜你喜欢
        • 2018-04-14
        • 2015-10-10
        • 1970-01-01
        • 1970-01-01
        • 2011-07-13
        • 1970-01-01
        • 2012-11-22
        • 1970-01-01
        • 2013-07-02
        相关资源
        最近更新 更多