【发布时间】:2013-08-11 17:11:16
【问题描述】:
您好,我正在尝试用我的脚本修复一个错误。问题是我的 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>");
}
【问题讨论】:
-
obviously it wouldn't- 为什么不呢? -
我记得最近读过一些东西,ID 不应该以数字开头,也不推荐。至于“为什么”,我不知道。我只记得读过一些有这种影响的东西,答案来自一个 38k 会员。
-
只有字符串需要转义(因此是“mysql_real_escape_string”而不是“mysql_real_escape_anything”)。对于数值,您最好使用
$idtoedit = intval($_GET['id']);,这也有助于理解您所询问的行为。 -
你应该使用 mysqli_* 函数。 mysql_* 已弃用。
-
如果有帮助,您应该接受其中一个答案。