编码问题?

发布于 2024-09-19 20:18:57 字数 551 浏览 3 评论 0原文

我认为我有 URL 编码问题。我需要使用 Javascript 打开一个窗口并向其传递 SQL Select 查询。所以我这样做了:

window.open('view_query.php?sql_query=' + query + '&db_name=' + db_name);

这对我有用,但我有一个查询打破了这一点:

SELECT a FROM table WHERE field like '%adhoc%' 

现在,当新窗口打开并打印从 URL GET 变量收到的查询时,它看起来像这样:

SELECT a FROM table WHERE field like '�hoc%' 

注意位 % ad 已变成无法识别的字符!为什么?

我曾尝试使用 URL 编码来解决此问题,但由于我需要 % 符号,因此我无法使用许多 URL 编码器,因为它们会将其转换为其他内容?!

感谢大家的帮助。

I think I have a URL encoding issue. I need to open a window using Javascript and pass a SQL Select query to it. So I have done this:

window.open('view_query.php?sql_query=' + query + '&db_name=' + db_name);

This has worked for me, but I have a query that breaks this:

SELECT a FROM table WHERE field like '%adhoc%' 

Now when the new window opens and I print the query received from the URL GET variable it looks like this:

SELECT a FROM table WHERE field like '�hoc%' 

Notice the bit %ad has turned into an unrecognised character! Why?

I have tried solving this with URL encoding but since I need the % symbol I can't use many URL encoders since they will turn this into something else?!

Thanks all for any help.

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

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

发布评论

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

评论(3

找回味觉 2024-09-26 20:18:57

% 字符用于使用字符代码对 URL 中的字符进行编码。序列 %ad 表示具有十六进制字符代码 0xAD 或十进制 173 的字符。

使用encodeURIComponent 函数转义 URL 的值:

window.open('view_query.php?sql_query=' + encodeURIComponent(query) + '&db_name=' + encodeURIComponent(db_name));

只是为了确保您(以及阅读本文的任何人)都知道其中,我还要指出通过浏览器发送 SQL 代码的风险。使用该系统的任何人都可以发送任何内容作为查询,包括例如删除表

The % character is used to encode characters in an URL using a character code. The sequence %ad means the character with the hexadecimal character code 0xAD, or decimal 173.

Use the encodeURIComponent function to escape values for the URL:

window.open('view_query.php?sql_query=' + encodeURIComponent(query) + '&db_name=' + encodeURIComponent(db_name));

Just to make sure that you (and anyone reading this) are aware of it, let me also point out the risks of sending SQL code via the browser. Anyone using the system could send anything as a query, including for example drop table.

你与清晨阳光 2024-09-26 20:18:57

使用 http://pl.php.net/urlencode 对您的查询进行编码。然后使用 http://pl.php.net/urldecode 对其进行解码(如果需要,PHP 应该这样做自动为您提供)

Encode your query using http://pl.php.net/urlencode. Then decode it using http://pl.php.net/urldecode (if needed, PHP should do this for you automatically)

南笙 2024-09-26 20:18:57

这是因为 url 编码 - 你需要调用
window.open('view_query.php?sql_query='+encodeURICompoent(query)+'&db_name...);

然后在 php 端
$query = rawurldecode($_GET['sql_query']);

This is because of url encoding - you need to call
window.open('view_query.php?sql_query='+encodeURICompoent(query)+'&db_name...);

and then on php side
$query = rawurldecode($_GET['sql_query']);

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