您好,我正在尝试修复我的脚本中的错误。问题是我的 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/