php - 奇怪的 $_GET 行为

标签 php mysql get

您好,我正在尝试修复我的脚本中的错误。问题是我的 get 查询仍然适用于额外的字母。

因此,edit.php?id=1 可以工作,edit.php?id=1hello 也可以(显然不应该)。发生什么事了?

$idtoedit = mysql_real_escape_string($_GET["id"]);

//Check if ID exists
$doesidexist = mysql_query("SELECT `id` FROM Data WHERE `id` = \"$idtoedit\"");
if (mysql_num_rows($doesidexist) == 0) {
    die("<div class=\"alert alert-error\"><h4 class=\"alert-heading\">Error</h4><p>ID does not exist.</p><p><a class=\"btn btn-danger\" href=\"javascript:history.go(-1)\">Go Back</a></p></div></div></body></html>");
 }

最佳答案

这是因为在将字符串转换为数字时,MySQL 对输入的“一切皆有可能”处理。

您构建的 SQL 查询将输入作为字符串(其周围的 ")传递,因此 mysql 会尝试将其转换为数字,如果左侧的列= 具有数字类型(示例中的 id 列)。

检查this section of the manual欲了解更多信息。

关于php - 奇怪的 $_GET 行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18174515/

相关文章:

php - 将对象数组存储到mysql数据库

php - SQLSTATE[HY000] : General error: 2006 MySQL server has gone away creating temporary table

php - 将 php $_GET 传递给 flash 播放器 xml 文件

php - Mysql_fetch_array,assoc,row 在从 db 获取后对数组进行排序

Jquery Ajax/GET 方法成功时没有任何反应

ios - Swift:转义闭包捕获非转义参数 'onCompletion'

php - 更新数据库时缺少参数错误

php - 如何在 PHP 中生成字符串的所有排列?

php - drupal--为什么 $node->taxonomy 是一个数组

php - Laravel 多个数据库连接不起作用