mysql_real_escape_string() 不清理变量

发布于 2024-10-31 01:16:39 字数 711 浏览 1 评论 0原文

我正在开发一个现有网站,试图防止 SQL 注入。在 $_GET['ID'] 未清理之前。

$ID=mysql_real_escape_string($_GET['ID']);
$sQuery=mysql_query("select * from tbl_mini_website as s1, tbl_actor_merchant as me where s1.MERCHANT_ID=$ID AND s1.MERCHANT_ID=me.MERCHANT_ID");

如果我在 url 末尾加上 ' ,使用 mysql_real_escape_string() ,我会从 mysql_error() 得到:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行 '\\' AND s1.MERCHANT_ID=me.MERCHANT_ID' 附近使用的正确语法

而不使用 mysql_real_escape_string() 我得到:

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在第 1 行 '\' AND s1.MERCHANT_ID=me.MERCHANT_ID' 附近使用的正确语法

我不确定这是怎么回事?任何帮助将不胜感激。

I'm working on an existing website trying to prevent SQL injections. Before $_GET['ID'] was unsanitized.

$ID=mysql_real_escape_string($_GET['ID']);
$sQuery=mysql_query("select * from tbl_mini_website as s1, tbl_actor_merchant as me where s1.MERCHANT_ID=$ID AND s1.MERCHANT_ID=me.MERCHANT_ID");

If I put a ' at the end of the url, with mysql_real_escape_string() I get this from mysql_error():

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\\' AND s1.MERCHANT_ID=me.MERCHANT_ID' at line 1

with out mysql_real_escape_string() I get:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\' AND s1.MERCHANT_ID=me.MERCHANT_ID' at line 1

I'm not sure whats up with it? Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不喜欢何必死缠烂打 2024-11-07 01:16:39

如果它是一个 id,我假设是数字,为什么不将它转换为整数呢?

$ID = (int) $_GET['ID'];

我能给你的最好建议是查看 PDO 并使用绑定参数。

If it is an id, numerical I assume, why don't you just cast it to an integer?

$ID = (int) $_GET['ID'];

The best advice I can give you is to check out PDO and use bound parameters.

〃温暖了心ぐ 2024-11-07 01:16:39

mysql_real_escape_string 转义,但不引用。

尝试:

$sQuery=mysql_query("select * from tbl_mini_website as s1, tbl_actor_merchant as me where s1.MERCHANT_ID='$ID' AND s1.MERCHANT_ID=me.MERCHANT_ID");

更一般地,我倾向于将这两者包装在一个函数中,例如:

function quoteValue($value) {
    return "'" . mysql_real_escape_string($value) . "'";
}

这很有用,因为您可能会发现您想要更精致的引用行为(特别是在处理 Unicode、控制字符等时)。 )

mysql_real_escape_string escapes, but doesn't quote.

Try:

$sQuery=mysql_query("select * from tbl_mini_website as s1, tbl_actor_merchant as me where s1.MERCHANT_ID='$ID' AND s1.MERCHANT_ID=me.MERCHANT_ID");

More generally, I tend to wrap both of these in a function, like:

function quoteValue($value) {
    return "'" . mysql_real_escape_string($value) . "'";
}

This is useful, because you may find down the line that you want more refined quoting behavior (especially when it comes to handling Unicode, control characters, etc.)

糖果控 2024-11-07 01:16:39

这是因为您没有引用该变量。

这是给定以下输入的查询

$_GET['ID'] = "1";
$ID=mysql_real_escape_string($_GET['ID']);
SELECT ... where s1.MERCHANT_ID=1 ...

$_GET['ID'] = "1'"
$ID=mysql_real_escape_string($_GET['ID']);
SELECT ... where s1.MERCHANT_ID=1\' ...

$_GET['ID'] = "1'"
SELECT ... where s1.MERCHANT_ID=1' ...

It's because you're not quoting the variable.

Here's your query given the following inputs

$_GET['ID'] = "1";
$ID=mysql_real_escape_string($_GET['ID']);
SELECT ... where s1.MERCHANT_ID=1 ...

$_GET['ID'] = "1'"
$ID=mysql_real_escape_string($_GET['ID']);
SELECT ... where s1.MERCHANT_ID=1\' ...

$_GET['ID'] = "1'"
SELECT ... where s1.MERCHANT_ID=1' ...
南薇 2024-11-07 01:16:39

Phil Brown 是对的,但是你应该忘记老式的 mysql_real_escape_string 或 mysql_connect() 因为它们已经很老了,并转移到 php 的 PDO() ,你可以在其中使用准备好的语句、绑定、获取任何对象还有更多的功能。

我建议阅读 PDO 文档 http://php.net/manual/en/book.pdo .php 如果您想要下一代数据库操作和 SQL 注入的安全性。

Phil Brown is right, but you shoul forget about old fashioned mysql_real_escape_string or mysql_connect() as they are very old and move to php`s PDO() where you cand use prepared statements, binds, fetch object any many many more functions.

I suggest read PDO documentation at http://php.net/manual/en/book.pdo.php if you want next generation dabatase manipulation and security from SQL Injection .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文