如何使用 php 将 HTML 代码插入到数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24631088/
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
how to insert HTML code into DB using php
提问by Dkova
i want to update my database with new html code this it the query:
我想用新的 html 代码更新我的数据库,这是查询:
UPDATE `Pages` SET `content`= '<div id="intro">
<div id="about" align="left">
<h2 class="bigHeader" dir="rtl"HEADER</h2>
<img src="img/Med-logo.png" alt="" />
<div id="wellcomePage" class="text-left text" dir="rtl">
<p>...some words....</p>
<p>.some words....</p>
<p> </p>
</div>
</div>
</div>'
but all the time i get an error. how can i update my database, i don't know what will be inside this html code, is there a function that make all the code like string without special sign?
但我总是遇到错误。我如何更新我的数据库,我不知道这个 html 代码里面会有什么,是否有一个函数可以让所有的代码像没有特殊符号的字符串一样?
EDIT:: the problem is with the special char like ' i can't change the html code, is user chice to put it.
编辑:: 问题出在特殊字符上,例如 ' 我无法更改 html 代码,用户是否可以放置它。
回答by MRJethva
Do following using addslashes()
function, so it will help easily to insert update html to
使用addslashes()
函数执行以下操作,以便轻松插入更新 html 到
UPDATE `Pages` SET `content`= addslashes('<div id="intro">
<div id="about" align="left">
<h2 class="bigHeader" dir="rtl"HEADER</h2>
<img src="img/Med-logo.png" alt="" />
<div id="wellcomePage" class="text-left text" dir="rtl">
<p>...some words....</p>
<p>.some words....</p>
<p> </p>
</div>
</div>
</div>')
回答by DWX
Try this:- $htmlcode = mysql_real_escape_string($htmlcode);
试试这个:- $htmlcode = mysql_real_escape_string($htmlcode);
For example:-
例如:-
$htmlcode = '<div id="intro">
<div id="about" align="left">
<h2 class="bigHeader" dir="rtl"HEADER</h2>
<img src="img/Med-logo.png" alt="" />
<div id="wellcomePage" class="text-left text" dir="rtl">
<p>...some words....</p>
<p>.some words....</p>
<p> </p>
</div>
</div>
</div>';
$htmlcode = mysql_real_escape_string($htmlcode);
UPDATE `Pages` SET `content`= '$htmlcode';
回答by GTS Soft.
store your html content in one variable and use addslashes() when you are inserting it to database.
将您的 html 内容存储在一个变量中,并在将其插入数据库时使用 addlashes()。
$content='<div id="intro">
<div id="about" align="left">
<h2 class="bigHeader" dir="rtl"HEADER</h2>
<img src="img/Med-logo.png" alt="" />
<div id="wellcomePage" class="text-left text" dir="rtl">
<p>...some words....</p>
<p>.some words....</p>
<p> </p>
</div>
</div>
</div>';
and write your query as below
并写下您的查询如下
UPDATE `Pages` SET `content`=addslashes($content);
Hope this will help you :)
希望能帮到你 :)
回答by Gleeb
I assume this will do the trick
我认为这会解决问题
when you get the text back from the database just decode it back
当您从数据库中取回文本时,只需将其解码回来
回答by ExaMuff
回答by S A M
I think there should be a (') single Quote into your string.
我认为您的字符串中应该有一个 (') 单引号。
You can use the 'htmlspecialchars' function with ENT_QUOTES as second argument.
您可以使用带有 ENT_QUOTES 作为第二个参数的 'htmlspecialchars' 函数。
And also 'mysql_real_escape_string' function can be used.
也可以使用 'mysql_real_escape_string' 函数。
Like
喜欢
$hcode = '<div id="intro">
<div id="about" align="left">
<h2 class="bigHeader" dir="rtl"HEADER</h2>
<img src="img/Med-logo.png" alt="" />
<div id="wellcomePage" class="text-left text" dir="rtl">
<p>...some words....</p>
<p>.some words....</p>
<p> </p>
</div>
</div>
</div>';
$hcode = htmlspecialchars($hcode, ENT_QUOTES);
UPDATE `Pages` SET `content`= '$hcode';