php 将 html 格式的文本保存到数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4577904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:32:55  来源:igfitidea点击:

save html-formatted text to database

phphtmlphpmyadmin

提问by yozhik

I want to save html-formatted text to database, but when I do that it is don't save html-symbols like < / > ' and others This is how I read article from database for editing:

我想将 html 格式的文本保存到数据库中,但是当我这样做时,它不保存 html 符号,如 </ > ' 和其他这是我从数据库中读取文章进行编辑的方式:

<p class="Title">Англ?йський вар?ант:</p>
    <textarea name="EN" cols="90" rows="20" value="<?php echo htmlentities($articleArr['EN'], ENT_QUOTES, "UTF-8"); ?>" ></textarea>

after this generates such html-code:

在生成这样的 html 代码之后:

<p class="Title">Англ?йський вар?ант:</p>
    <textarea name="EN" cols="90" rows="20" value="&lt;p class=&#039;Title&#039;&gt; привыт &lt;/p&gt;" ></textarea>

So, I expect that this text will appear in my text field, in html-code of this page it is, but in text area is no.

所以,我希望这个文本会出现在我的文本字段中,在这个页面的 html 代码中是,但在文本区域中不是。

In database I save it as:

在数据库中,我将其另存为:

<p class="Title"> Hello </p>

So how can I do the follow:

那么我该如何执行以下操作:

  1. Read from database html-formattedtext.
  2. Show it in textarea element.
  3. Edit and save it back to database.
  1. 从数据库 html 格式的文本中读取。
  2. 在 textarea 元素中显示它。
  3. 编辑并将其保存回数据库。

Help me please, how can I save such texts properly, Thanx!

请帮帮我,我怎样才能正确保存这些文本,谢谢!

回答by Phoenix

Try using htmlspecialchars()on the string to put into the DB, and then, when pulling it back out, use htmlspecialchars_decode(). Might make a difference.

尝试htmlspecialchars()在字符串上使用以放入数据库,然后在将其拉回时使用htmlspecialchars_decode(). 可能会有所作为。

回答by Panindra

Save it to a nvarchar(max) field.

将其保存到 nvarchar(max) 字段。

Make sure you use parameterized queries for security. Read

确保使用参数化查询来确保安全。读

http://www.aspnet101.com/2007/03/parameterized-queries-in-asp-net/

http://www.aspnet101.com/2007/03/parameterized-queries-in-asp-net/

http://msdn.microsoft.com/msdnmag/issues/04/09/SQLInjection/

http://msdn.microsoft.com/msdnmag/issues/04/09/SQLInjection/

with little changes to Sql , you can apply to Mysql aslo

对 Sql 稍加改动,您就可以申请到 Mysql aslo