简体   繁体   English

将html内容保存在mysql数据库中

[英]saving html content in mysql database

I am using php and trying save some html contents in mysql database. 我正在使用php并尝试在mysql数据库中保存一些html内容。 the html content is generating by ckeditor. html内容是由ckeditor生成的。 The content is something like this- 内容是这样的-

<p><img align="left" alt="" src="images/1im1.jpg" style="margin:1px 15px 0 0; border:1px solid #cecece; " /> <img alt="If syou love hot sauce" src="images/tit_If-you-love-hot-sauce.jpg" /></p><br>D'elidas is a fine<p>

I am using this in php- 我在php-中使用它

$main_data = mysql_real_escape_string($_POST['content']);

This was working okay in my localhost(xampp). 这在我的本地主机(xampp)中工作正常。 but not working in online. 但不能在线工作。 my hosting is using latest version of PHP and MySQL. 我的主机正在使用最新版本的PHP和MySQL。 after saving in online database, I see like this- 保存在在线数据库中后,我看到这样的

<p><img align=\"left\" alt=\"\" src=\"images/1im1.jpg\" style=\"margin:1px 15px 0 0; border:1px solid #cecece; \" /> <img alt=\"If syou love hot sauce\" src=\"images/tit_If-you-love-hot-sauce.jpg\" /></p>br>D\'elidas is a fine<p> 

And that is why the HTML is not displaying correctly in my page. 这就是HTML无法在我的页面中正确显示的原因。 Please help me about this. 请帮助我。 this is adding slashes before quotes. 这是在引号之前添加斜线。 I want to save exact html and show in front end. 我想保存确切的html并在前端显示。

You hosting company probably has magic quotes turned on - http://php.net/manual/en/security.magicquotes.php 您的托管公司可能已启用魔术引号-http: //php.net/manual/en/security.magicquotes.php

You can't disable it in code, but Example 2 here shows a work around http://www.php.net/manual/en/security.magicquotes.disabling.php 您无法在代码中将其禁用,但是示例2在此处显示了围绕http://www.php.net/manual/zh/security.magicquotes.disabling.php的工作

It sounds like your host probably has magic_quotes_gpc turned on, which will automatically add slashes to quotes and double quotes on data coming in from $_GET, $_POST, and $_COOKIE. 听起来您的主机可能已打开magic_quotes_gpc ,它将对来自$ _GET,$ _ POST和$ _COOKIE的数据自动在引号和双引号中添加斜杠。

You might want to create a wrapper function for escaping GPC data. 您可能需要创建一个包装函数来转义GPC数据。 As an example... 举个例子...

function mysql_escape_gpc($dirty)
{
    if (ini_get('magic_quotes_gpc'))
    {
        return $dirty;
    }
    else
    {
        return mysql_real_escape_string($dirty);
    }
}

This way your code is portable, regardless of how the server is configured. 这样,无论服务器如何配置,代码都可移植。

Also, if your production environment supports it, you should consider looking into prepared statements. 另外,如果您的生产环境支持它,则应考虑研究准备好的语句。 This way you don't have to worry about escaping your data, however you would still need to UNescape it in the event that magic_quotes_gpc is turned on. 这样,您不必担心转义数据,但是在magic_quotes_gpc打开的情况下,仍然需要取消转义数据。

I accomplished this by using the following code segments in php and mySQL database: 我通过在php和mySQL数据库中使用以下代码段来完成此任务:

Storing into the database. 存储到数据库中。 You must use the following code segment in the actual mySQL Insertcall. 您必须在实际的mySQL Insertcall中使用以下代码段。 I found out if you do this to the variable first and then put the variable in the insert call it will not work. 我发现如果您先对变量执行此操作,然后将其放入插入调用中,它将不起作用。 The function must be in the mySQL statement. 该函数必须在mySQL语句中。
mysql_real_escape_string($myValue)

Retrieving Into textbox in value. 检索到文本框中的值。 Assuming your values have been already retrieved from the database and now are in an array Called theValues. 假设您的值已经从数据库中检索出来,现在位于名为theValues的数组中。 Basically I am Removing any backslashes but before hand I'm making sure it can be displayed correctly using htmlentities. 基本上,我正在删除任何反斜杠,但是在使用之前,我确保可以使用htmlentities正确显示它。 Since you are no Backslashes in HTML that I know of it fixes it where servers replace quotes with \\". If you do encounter some Back slashes in HTML you'll just have to be a bit more clever in your replacement function. 因为您在HTML中没有反斜杠,所以我知道它可以解决服务器将引号替换为\\“的问题。如果确实遇到了HTML中的反斜杠,则您的替换功能必须更加聪明。
$myValue= str_replace("\\\\", "", htmlentities($theValues->myValue)); echo $myValue;

echoing out on to a page same reasons as above, but the htmlentities function Makes it only display the text of the HTML Instead of processing the HTML 回显到页面的原因与上述相同,但htmlentities函数使其仅显示HTML的文本,而不处理HTML
str_replace("\\\\", "",$myValue)

When you fetch it from the database you need to run a stripslashes() on the HTML string. 从数据库中获取它时,您需要在HTML字符串上运行stripslashes()。 Right? 对?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM