Php 在mysql表中插入html代码

Php 在mysql表中插入html代码,php,html,mysql,insert,Php,Html,Mysql,Insert,我用joomla管理一个网站。。。我正在开发一个独立的php应用程序,它将向joomla用来存储动态创建的网页html的表中插入和修改数据 它的工作方式是我使用joomla组件创建内容,这些文章的html代码存储在joomla的表中的一个字段中,比如content_table。。稍后检索此html代码以构建网页的一部分 我想对我的独立应用程序执行相同的操作。。。i、 e将html代码添加到content_表中,joomla稍后可以检索到该表以构建页面的一部分 问题是:当然,html代码有很多单引

我用joomla管理一个网站。。。我正在开发一个独立的php应用程序,它将向joomla用来存储动态创建的网页html的表中插入和修改数据

它的工作方式是我使用joomla组件创建内容,这些文章的html代码存储在joomla的表中的一个字段中,比如content_table。。稍后检索此html代码以构建网页的一部分

我想对我的独立应用程序执行相同的操作。。。i、 e将html代码添加到content_表中,joomla稍后可以检索到该表以构建页面的一部分


问题是:当然,html代码有很多单引号和双引号,这在插入数据库时会造成问题。。我尝试了mysql\u escape\u string(),但仍然出现语法错误

我可以使用addslashes(),但由于joomla本身会在稍后检索代码,因此无法在稍后检索代码时使用stripslashes()

我是否可以在表格的字段中添加html代码

谢谢你的建议

编辑:添加mysql\u escape\u string()后,我得到

这是我的疑问:

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;">
&nbsp;
</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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :
</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&nbsp;&nbsp;&nbsp;
</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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</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&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</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}
&nbsp;
</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>
&nbsp;High Bandwidth Users
</b>
</b>
</span>
</i>
<i>
<span style="color: rgb(102, 255, 0);">
<b>
<b>
&nbsp;
</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>
&nbsp;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=&quot;235&quot; displayheight=&quot;0&quot; height=&quot;225&quot;} <?php echo $hqList; ?> {/auto}
</td>
<td>
{auto width=&quot;235&quot; displayheight=&quot;0&quot; height=&quot;225&quot;}<?php echo $lqList; ?>{/auto}
</td>
</tr>
</tbody>
</table>
<center>
{loadposition user50}
</center>';
$article\u code=”
{loadposition user50}


"

演员阵容:
方向 :
生产 :
音乐 :
歌词 :
年 :

{loadposition user14}

点击 在播放列表中下载歌曲

高带宽用户 低带宽用户
{auto width=“235”displayheight=“0”height=“225”}{/auto} {auto width=“235”displayheight=“0”height=“225”}{/auto} {loadposition user50} ';
您不需要斜杠。在正常插入过程中,唯一会导致问题的是引号,
mysql\u escape\u string()
应该处理除字符集问题之外的其他问题。请尝试
mysql\u real\u escape\u string()


也注意到,将原始用户提供的HTML存储在数据库中会导致安全问题。考虑使用BBoCK或BaskDon之类的东西。

< P>只是确认,您的查询看起来是这样的:

$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).'"
";

嗯..调试了..结果问题毕竟不是转义函数的问题

查看查询:

UPDATE $jos_content
SET    introtext = '$intro_code',
       fulltext  = '$article_code'
WHERE  id = '$article_id'";
您可以看到“全文”字段…显然,“全文”是一个mysql关键字…准确地说,它是一个字段类型,如TEXT、INT、MEDIUMTEXT等

我将查询更改为此

"UPDATE $jos_content
SET    $jos_content.introtext = '$intro_code',
       $jos_content.fulltext  = '$article_code'
WHERE  $jos_content.id = '$article_id'";

在插入数据库之前,我更喜欢将代码转换成普通字符串。我认为,这是最安全的方案。考虑使用这个代码:

$article_code = base64_encode($article_code);
/* insert to database */

因此,当您想使用该代码返回时,只需使用base64_decode进行解码。我建议您使用'text'数据类型来保存$article_代码,而不是'varchar'。

fulltext
是mysql预定义的关键字。请使用锐(`)或单引号(')

这是代码-

UPDATE $jos_content
SET    `introtext` = '$intro_code',
       `fulltext`  = '$article_code'
WHERE  `id` = '$article_id'";

我也有同样的问题,我用正则表达式解决了它。 您可以这样使用:
$target='{~p class={{{{q}}}重要文本{{q}}}}这里的一些文本{~/p};

然后使用
preg\u 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>
类句柄
{ 
公共静态函数makehtml($target)
{
$output=preg_replace(“#{~#“,”,$target);
$output=preg_replace(“#{{q}}}#”,“''”,$target);
返回$output;
}  
}  
echo句柄::makehtml($target);
//输出:

此处有一些文本


如果您担心空间问题,并且正在使用此处发布的base 64 encode方法,您可以使用php中的gzdeflate命令将其缩减,然后对其进行编码。下面是一些生成的示例字符的测试脚本。如果您关心大小,显然有不同的压缩方法可供研究,但这意味着你可以把它放到mysql中,最小化数据库的大小。然后你可以用gzinflate(base64_decode(…))读回它


调用mysql\u escape\u string()函数,传入保存html文本的变量,如:

mysql_escape_字符串($_POST[“text”])


将确保文本中的特殊字符(如引号)不会导致php错误,并且数据库将成功更新。

这是我找到的最佳方法
addslashes()


将html代码插入数据库

$title = $_POST['title'];
$code = '<h1>hello</h1></p>this html tage</p>';
$htmlcode = addslashes($code);

 $query = "INSERT INTO `template` (`title`,`code`) VALUES ('".$title."','".$htmlcode."');"; 
mysqli_query($con,$query);

如果您正在使用mysqli,它需要一个连接

mysqli_real_escape_string($conn, $_POST["name"]);

“我尝试了mysql\u escape\u string(),但仍然出现语法错误”例如?我是codaddict的——除非
mysql\u escape\u string
函数中出现错误,否则你不应该出现任何错误,我对此深表怀疑。你能打印出导致此错误的
mysql\u escape\u string
的输入和输出吗?太长了……我还是会发布它……我认为这是个坏主意。你的应用程序n以这种方式从数据库耦合到UI。根本没有分层。我会重新考虑它,除非PHP强制您进行这种安排。BBcode不是这里的选项。因为joomle只是在字段中获取条目并在页面中输出它…!!它不做任何类型的解析…确保您的数据类型在本质上是NVARCHARY,而不是fie类型ld.这是一种索引。我相信你也可以通过将字段名括在后面的记号(
`
)来避免这个问题。你说得对,唯一的缺点是这会使你的数据增加33%,但这是一个KISS解决方案!现在你让你的数据库无法搜索。
$article_code = addslashes($article_code);

UPDATE $jos_content
SET    introtext = '$intro_code',
       fulltext  = '$article_code'
WHERE  id = '$article_id'";
$title = $_POST['title'];
$code = '<h1>hello</h1></p>this html tage</p>';
$htmlcode = addslashes($code);

 $query = "INSERT INTO `template` (`title`,`code`) VALUES ('".$title."','".$htmlcode."');"; 
mysqli_query($con,$query);
$result = mysqli_query($con,'SELECT * FROM template');                                  
while($row = mysqli_fetch_assoc($result)) {

 echo htmlentities($row['code']);

}
htmlentities($row['code']); //here your row name;
mysqli_real_escape_string($conn, $_POST["name"]);