存储在MySQL表中时,PHP / MySQL代码被切断

时间:2011-11-14 11:39:24

标签: php mysql

我需要更新MySQL数据库中的某些字段。这些字段包含PHP和HTML代码。我知道这很危险。但是,应用程序在Intranet上运行,因此我不想麻烦这个讨论。

我的问题是,(对我来说)在数据库中获取代码似乎是不可能的。部分字符串被截断。我的部分代码(出现问题)如下:

$sql = ("UPDATE `database`.`table` SET `code` = '<iframe width='100%' height='100%' src='http://URL?rel=0&autoplay=1' frameborder='0' allowfullscreen></iframe>') WHERE `table`.`id` =4 LIMIT 1 ;"); 

问题始于第一个'&lt;'第一个'iframe'。运行它时,不会解释它背后的一切。当我用一个单词替换代码时,即'test',代码被正确解释并保存得很好。

非常感谢您解决这个问题的任何帮助 罗布

2 个答案:

答案 0 :(得分:1)

尝试逃避HTML代码中的叛逆者

$sql = ("UPDATE `database`.`table` SET `code` = '<iframe width=\'100%\' height=\'100%\' src=\'http://URL?rel=0&autoplay=1\' frameborder=\'0\' allowfullscreen></iframe>') WHERE `table`.`id` =4 LIMIT 1 ;";

或稍微简单一些:

$sql = ("UPDATE `database`.`table` SET `code` = \"<iframe width='100%' height='100%' src='http://URL?rel=0&autoplay=1' frameborder='0' allowfullscreen></iframe>\") WHERE `table`.`id` =4 LIMIT 1 ;";

此外,paranthesys看起来错了:

$sql = "UPDATE `database`.`table` SET `code` = \"<iframe width='100%' height='100%' src='http://URL?rel=0&autoplay=1' frameborder='0' allowfullscreen></iframe>\" WHERE `table`.`id` =4 LIMIT 1 ;";

答案 1 :(得分:0)

使用函数htmlentities()

$orig = "I'll \"walk\" the <b>dog</b> now"

$a = htmlentities($orig);

echo $a; // I'll &quot;walk&quot; the &lt;b&gt;dog&lt;/b&gt; now;

和html_entity_decode()

$b = html_entity_decode($a);

echo $b; // I'll "walk" the <b>dog</b> now