在mysql表格中插入html代码
我使用joomla来管理网站......我正在开发一个独立的php应用程序,它将插入和修改数据到joomla使用的表格中,以存储网页的html它动态创建...在mysql表格中插入html代码
它的工作方式是我使用joomla组件来创建内容和这些文章的HTML代码存储在一个表中的字段,说content_table,通过joomla ..此html代码是稍后检索以构建网页的一部分。
我想要对我的独立应用做同样的处理......即将html代码添加到content_table中的字段中,该代码稍后可以通过joomla检索以构建页面的一部分。
问题是:当然,html代码当然有很多单引号和双引号,并且在插入数据库时会造成问题。我试过mysql_escape_string(),但仍然出现语法错误。 。
我可以使用和addslashes(),但由于自身的Joomla获取码后,它不可能用stripslashes(),而后来取回....
反正我有可以添加HTML代码桌子的领域...
感谢您的建议......!
编辑:添加函数mysql_escape_string()后,我得到
Error adding details. Reason : 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 'fulltext = '\n
这是我的查询:
UPDATE $jos_content
SET introtext = '$intro_code',
fulltext = '$article_code'
WHERE id = '$article_id'";
输入字符串如下:
$article_code = '<hr id="system-readmore" />
<center>{loadposition user50}</center>
<p style="text-align: center;">
<span style="color: rgb(0, 255, 255);">
<i>
<b>
<span style="font-size: x-large;">
<span style="font-family: Arial;">
</span>
</span>
</b>
</i>
</span>
<span style="color: rgb(0, 255, 255);">
<i>
<b>
<span style="font-size: x-large;">
<span style="font-family: Arial;">
<?php echo $title; ?>
</span>
</span>
</b>
</i>
</span>
<span style="color: rgb(0, 255, 255);">
<i>
<b>
<span style="font-size: x-large;">
<span style="font-family: Arial;">
<br />
</span>
</span>
</b>
</i>
</span>
</p>
<p style="text-align: center;">
<img height="269" width="515" border="3"
title="<?php echo $title; ?>"
alt=" <?php echo $title; ?>"
src="<?php echo $article_image;?>"
</p>
<p>
<span style="font-size: small;">
<span style="font-family: Arial;">
<span style="color: rgb(153, 204, 255);">
<p style="margin-top: 2px; margin-bottom: 2px; margin-left: 120px; text-align: left;">
<i>
<span style="color: rgb(0, 255, 0);">
<strong>
Cast :
</strong>
<b>
</b>
</span>
</i>
<span style="color: rgb(0, 255, 255);">
<b>
<?php echo $cast; ?>
</b>
</span>
<i>
<span style="color: rgb(0, 255, 255);">
<b>
<br />
</b>
</span>
</i>
<span style="font-family: Arial;">
<span style="font-size: small;">
<span style="color: rgb(153, 204, 255);">
</span>
</span>
<span style="color: rgb(0, 255, 0);">
<i>
<strong>
Direction
</strong>
</i>
<strong>
:
</strong>
<b>
</b>
</span>
<span style="color: rgb(0, 255, 255);">
<b>
<span class="href"
id="ctl00_ContentPlaceHolderMainContent_FormView1_Director">
<?php echo $director; ?>
</span>
</b>
</span>
</span>
<span style="font-family: Arial;">
<br />
<span style="color: rgb(0, 255, 0);">
<i>
<strong>
Production
</strong>
</i>
<strong>
:
</strong>
<b>
</b>
</span>
<span style="color: rgb(0, 255, 255);">
<b>
<?php echo $direction; ?>
</b>
</span>
<span style="color: rgb(255, 102, 0);">
<i>
<b>
<br />
</b>
</i>
</span>
<span style="font-family: Arial;">
<span style="color: rgb(0, 255, 0);">
<span style="font-family: Arial;">
<span style="font-size: small;">
<i>
<strong>
Music
</strong>
</i>
<strong>
:
</strong>
</span>
</span>
</span>
</span>
<span style="color: rgb(0, 255, 255);">
<b>
<i>
</i>
<?php echo $music; ?>
<i>
<br />
<span style="color: rgb(0, 255, 0);">
Lyrics
</span>
</i>
<span style="color: rgb(0, 255, 0);">
:
</span>
<i>
</i>
</b>
</span>
<span style="color: rgb(0, 255, 255);">
<b>
<?php echo $lyrics; ?>
</b>
</span>
<span style="color: rgb(0, 255, 255);">
<b>
<i>
<br />
</i>
<span style="color: rgb(0, 255, 0);">
<i>
Year
</i>
:
</span>
<?php echo $year; ?>
</b>
</span>
</span>
<i>
<span style="color: rgb(0, 255, 255);">
<b>
</b>
</span>
</i>
</p>
</span>
</span>
</span>
</p>
<p>
<left>
{loadposition user14}
</left>
</p>
<div style="text-align: center;">
<p>
<i>
<span style="font-family: Arial;">
<b>
<span style="font-size: medium;">
<span style="color: rgb(51, 255, 255);">
Click
<img src="images/stories/Play button1.png"
alt="alt" />
in the Playlist to Download Songs
</span>
</span>
</b>
</span>
</i>
</p>
</div>
<table border="0" align="center">
<tbody>
<tr>
<td>
<h4 style="text-align: center;">
<i>
<span style="color: rgb(102, 255, 0);">
<b>
<b>
High Bandwidth Users
</b>
</b>
</span>
</i>
<i>
<span style="color: rgb(102, 255, 0);">
<b>
<b>
</b>
</b>
</span>
</i>
<span style="color: rgb(102, 255, 0);">
<b>
</b>
</span>
</h4>
</td>
<td>
<h4 style="text-align: center;">
<i>
<span style="color: rgb(102, 255, 0);">
<b>
<b>
Low Bandwidth Users
</b>
</b>
</span>
</i>
<span style="color: rgb(102, 255, 0);">
<b>
<br />
</b>
</span>
</h4>
</td>
</tr>
<tr>
<td>
{auto width="235" displayheight="0" height="225"} <?php echo $hqList; ?> {/auto}
</td>
<td>
{auto width="235" displayheight="0" height="225"}<?php echo $lqList; ?>{/auto}
</td>
</tr>
</tbody>
</table>
<center>
{loadposition user50}
</center>';
Well..Debugged吧..原来,问题是毕竟不是脱出功能...
退房查询:
UPDATE $jos_content
SET introtext = '$intro_code',
fulltext = '$article_code'
WHERE id = '$article_id'";
你可以看到“全文”场......显然,这个词“全文”是一个MySQL的关键字......准确地说,这是一个字段类型,如文本,INT,MEDIUMTEXT等...
我改变了查询本
"UPDATE $jos_content
SET $jos_content.introtext = '$intro_code',
$jos_content.fulltext = '$article_code'
WHERE $jos_content.id = '$article_id'";
瞧...... !!!!
你不应该需要斜杠。在正常插入过程中唯一会导致问题的是引号,并且mysql_escape_string()
应该处理除charset问题外的其他问题。同样尝试mysql_real_escape_string()
。
此外,请注意,将原始用户提供的HTML存储在数据库中可能会导致安全问题。考虑使用类似bbcode或markdown的东西。
BBcode不是一个选项在这里..因为joomle只是获得了现场的条目,并将其输出到页面中!它不会做任何类型的解析...... – SpikETidE 2010-03-03 12:37:14
只是为了确认,您的查询看起来像这样的权利:
$query = '
UPDATE "'.mysql_real_escape_string ($jos_content).'"
SET introtext = "'.mysql_real_escape_string ($intro_code).'",
fulltext = "'.mysql_real_escape_string ($article_code).'"
WHERE id = "'.mysql_real_escape_string ($article_id).'"
";
我喜欢在插入数据库之前将代码转换为普通字符串。我认为,这是最安全的情况。请考虑使用此代码:
$article_code = base64_encode($article_code);
/* insert to database */
所以,当你想使用的代码,则只需用解码BASE64_DECODE。我建议你使用'text'数据类型来保存$ article_code而不是'varchar'。
你说得对,唯一的缺点就是会增加33%的数据,但是到目前为止,这是一个KISS解决方案! – 2012-06-11 14:50:56
现在你让你的数据库无法搜索。 – 2013-01-18 16:49:43
fulltext
是一个mysql预定义关键字。请使用急性(`)或单引号(')
这里是代码 -
UPDATE $jos_content
SET `introtext` = '$intro_code',
`fulltext` = '$article_code'
WHERE `id` = '$article_id'";
我有同样的问题,我用正则表达式固定它。 您可以使用像这样:$target = '{~p class={{q}}important-text{{q}}~}Some text here {~/p~}';
,然后使用preg_replace()
功能:
class handle
{
public static function makehtml($target)
{
$output = preg_replace("#{~#", "<", $target);
$output = preg_replace("#~}#", ">", $target);
$output = preg_replace("#{{q}}#", '"', $target);
return $output;
}
}
echo handle::makehtml($target);
// output : <p class="important-text">Some text here</p>
如果你担心空间和正在使用此发布的基地64编码方法,你可以在使用gzdeflate命令PHP将其剪下来然后进行编码。以下是一些生成的示例字符的测试脚本。显然有不同的压缩方法,如果你关心的大小,但这意味着你可以把它放到MySQL最小化数据库的大小。然后你可以用gzinflate(base64_decode(...))读回它;
<?php
//generate sample chars for the example
function generateRandomString($length = 10000) {
$characters = '"56/789abcdefghij:klmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
//
$string =generateRandomString();
echo 'string size:'.strlen($string);
$b64html = base64_encode($string);
echo '<br/>'.'Original base64 size:'.strlen($b64html);
$compressed = gzdeflate($string, 9);
echo '<br/>'.'compressed size:'.strlen($compressed);
echo '<br/>'.'base64 compressed size:'.strlen(base64_encode($compressed));
/* insert into db the base64_encode($compressed); */
?>
呼叫传递在保存的HTML文本等的变量的函数mysql_escape_string()函数:
函数mysql_escape_string($ _ POST [ “文本”]);
将确保文本中引号之类的特殊字符不会导致php错误,并且数据库将成功更新。
这是我发现addslashes()
$article_code = addslashes($article_code);
UPDATE $jos_content
SET introtext = '$intro_code',
fulltext = '$article_code'
WHERE id = '$article_id'";
只要你想使用该代码就使用stripslashes。 – 2018-02-13 05:34:09
“我试图函数mysql_escape_string(),仍然可以得到语法错误”的最佳途径。喜欢 ? – codaddict 2010-03-03 12:20:50
我与codaddict - 你不应该得到任何错误,除非有'mysql_escape_string'函数中的错误,我非常怀疑。 – 2010-03-03 12:23:05
你能打印出导致这个错误的'mysql_escape_string'的输入和输出吗? – 2010-03-03 12:24:26