当 URL 末尾有撇号时,为什么会出现 SQL 错误?

发布于 2024-11-30 02:19:06 字数 265 浏览 2 评论 0原文

为什么在 URL 末尾添加 ' 时会出现错误?例如:http://mywebsite.com/singel?id=24'

我收到以下错误:

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

如果我将 ' 放置在查询字符串。

出了什么问题,如何修复? 谢谢。

Why do I get an error when I add ' to the end of a URL? For example : http://mywebsite.com/singel?id=24'

I get the following 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 '\' LIMIT 1' at line 1

This is shown everywhere if I put ' after any id in the query string.

What is wrong, and how it can be fixed?
Thank you.

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

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

发布评论

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

评论(4

缘字诀 2024-12-07 02:19:06

您正在 SQL 查询中插入非转义变量。如果这个变量恰好包含 SQL 特殊字符,则可能会导致 SQL 语法错误或更糟糕的情况。

在将变量插入 SQL 查询之前,您需要转义它们。

示例:

$query = "SELECT * FROM users WHERE id = " . mysql_real_escape_string($id);

而不是(这是错误的,不要这样做):

$query = "SELECT * FROM users WHERE id = $id LIMIT 1";

如果 $id24',查询将变为

$query = "SELECT * FROM users WHERE id = 24' LIMIT 1";

:可以看到,24后面多了一个',这是语法错误。

You are inserting a non-escaped variable in an SQL query. And if this variable happens to contain SQL special chars, this can cause SQL syntax errors or worse.

You need to escape your variables before inserting them in your SQL queries.

Example:

$query = "SELECT * FROM users WHERE id = " . mysql_real_escape_string($id);

Instead of (this is WRONG, don't do this):

$query = "SELECT * FROM users WHERE id = $id LIMIT 1";

If $id is 24', the query becomes:

$query = "SELECT * FROM users WHERE id = 24' LIMIT 1";

As you can see, there is a ' after 24, which is a syntax error.

调妓 2024-12-07 02:19:06

如果 ' 终止了您的查询,则很明显您存在 SQL 注入漏洞。阅读 mysql_real_escape_string()bobby-tables,并考虑切换到 PDO 准备好的语句。

if a ' kills your query, you very obviously have an sql injection vulnerability. Read up on mysql_real_escape_string(), bobby-tables, and consider switching to PDO prepared statements.

揽清风入怀 2024-12-07 02:19:06

看这里

http://www.codeproject.com/KB/database/SqlInjectionAttacks.aspx

你应该了解一些有关 SQL 注入的知识。您的脚本现在可以注入了

Look here

http://www.codeproject.com/KB/database/SqlInjectionAttacks.aspx

You should learn something about SQL injection. Your Script is injectable now

茶底世界 2024-12-07 02:19:06

此错误通常意味着您对 sql 注入持开放态度。这个函数比 mysql_real_escape_string() 稍微复杂一点,因为它还做了 PHP 配置测试,以确保您不会两次转义数据并添加各种 PHP 版本支持! (根据 PHP 手册 1
只需在每个提交的项目上运行这个漂亮的小函数,用于插入、更新或 where 语句:

function sql_injection($value){
    $value = trim($value);
    if(get_magic_quotes_gpc())
        $value = stripslashes($value);
    if(function_exists("mysql_real_escape_string")){
        $value = mysql_real_escape_string($value);
    }else
        $value = addslashes($value);
    return $value;
}

例如:

$q = 'SELECT `blah` FROM `users` WHERE `id`='.sql_injection($_POST['id']);
  • 此函数不会取代服务器端验证,并且在使用它之前应该验证所有内容,始终假设最坏的情况:)

This error usually means you are open to sql injections. This function is a bit more complicated than mysql_real_escape_string(), because it also does a PHP configuration test, to make sure you do not escape the data twice and to add Various PHP version support! (as per PHP Manual 1)
Just run this little nifty function on each of the submitted items, for inserts, updates or where statements:

function sql_injection($value){
    $value = trim($value);
    if(get_magic_quotes_gpc())
        $value = stripslashes($value);
    if(function_exists("mysql_real_escape_string")){
        $value = mysql_real_escape_string($value);
    }else
        $value = addslashes($value);
    return $value;
}

Ex:

$q = 'SELECT `blah` FROM `users` WHERE `id`='.sql_injection($_POST['id']);
  • This function does NOT replace server side validation, and everything should be validated before using it, always assume the worst :)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文