【问题标题】:SyntaxError: unterminated string literal with whitespace onlySyntaxError:未终止的字符串文字,仅包含空格
【发布时间】:2016-09-19 17:37:12
【问题描述】:

我尝试从 javascript 函数中启动参数,但是当我有空格时,引号似乎有问题。

这是Mozilla的错误

SyntaxError: 未终止的字符串文字

这是我的代码

$requete = pg_query($dbconnect,"SELECT '<table id=''id1''><col width=''30%''/><col width=''20%''/><col width=''30%''/><col width=''20%''/><tr></tr><tr><td id=''fiche'' colspan=4>Caract</td></tr><tr><td>Stat</td><td>'||coalesce(stat,'')||'</td><td>Service1</td><td>'||coalesce(serv,'')||'</td></tr><tr><td>Date1</td><td>'||coalesce((substring(CAST(date1 AS character varying) from 9 for 2)||'/'||substring(CAST(date1 AS character varying) from 6 for 2)||'/'||substring(CAST(date1 AS character varying) from 1 for 4)),'')||'</td><td>Heure fin</td><td>'||coalesce(heure_fin,'')||'</td></tr><tr><td>Heure début</td><td>'||coalesce(heure_debut,'')||'</td><td>Durée</td><td>'||coalesce(duree::varchar,'')||'</td></tr><tr><td colspan=2><a onclick=document.getElementById(''affichage_popup'').style.display=''none''; class=''bouton'' id=''retour''>Retour</a></td><td colspan=2><a onclick=lancement(''script\\\php\\\postgres_query.php'',0,''DELETE FROM table WHERE id=2''); class=''bouton'' id=''supprimer''>Supprimer la fiche</a></td></tr></table>' AS fiche FROM table")

如果我用 Mozilla 分析返回的代码,我有这个

<a id="test')" where="" intervention="" from="" onclick="lancement('script/php/postgres_query.php',0,'DELETE" id="supprimer" class="bouton">Supprimer la fiche</a>

我试图用像 '/s' 这样的正则表达式替换空格,但它是一样的。

【问题讨论】:

  • 似乎错误在这里onclick="lancement('script/php/postgres_query.php',0,'DELETE",没有关闭) 还有为什么同一个锚标签上有两个id
  • 如果我写onclick=lancement(''script\\\php\\\postgres_query.php'',0,''DELETEFROMtableWHEREid=2'');,Mozilla 的代码返回是好的:onclick="lancement('script/php/postgres_query.php',0,''DELETEFROMtableWHEREid=2" 还有,我在 Id 和一个类在 balise &lt;a&gt;
  • 另外,值得注意的是代码极易受到 SQL 注入攻击。
  • 是的,我知道,但它是一个内部工具 ;-)
  • 我不认为这是一个很好的借口,但我只是指出了这一点。无论如何,在 PHP 中你正在做onclick=lancement(...),但如果你有一个带有空格的属性值,那么你需要将整个东西用引号引起来,所以你需要生成onclick="lancement(...)"(或单引号中的等价物)。浏览器向您显示的是它对节点最有可能的表示,而不是实际的 HTML。格式错误的 HTML 仍然可以生成 a 节点(浏览器对错误输入的容忍度很高),但这并不意味着它会起作用。

标签: javascript php


【解决方案1】:

你为什么不使用 pg_escape_string? http://php.net/manual/en/function.pg-escape-string.php

$requete = pg_query($dbconnect,"SELECT '".pg_escape_string("<table ... </table>")."' AS fiche FROM table");

但如果我没看错...你是在开玩笑吗?你在 HTML SQL 语句中使用的那个...所以每个人都可以更改你的语句并破解你的数据库...你听说过SQL Injection

【讨论】:

  • 不幸的是,有了这个函数,编写代码就更容易了,非常感谢!但这不是解决方案:-(同样的事情,但事实上在点击按钮之前我有这个代码&lt;a onclick="javascript:document.getElementById('affichage_popup').innerHTML = '&amp;lt;table (...) onclick=lancement(script/php/postgres_query.php,0,DELETE FROM table WHERE id=2)&amp;gt;Supprimer la fiche&amp;lt;/a&amp;gt;&amp;lt;/td&amp;gt;&amp;lt;/tr&amp;gt;&amp;lt;/table&amp;gt;';document.getElementById('affichage_popup').style.display='block';" href="#"&gt;36970&lt;/a&gt;,当我点击那个爆炸时......
  • 最后在您的帮助下,我选择使用变量而不是使用 SQL 注入。它更安全,最终更容易!感谢您的帮助
猜你喜欢
  • 2013-10-16
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 2022-07-09
  • 2017-02-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多