php 如何将html存储在mysql数据库中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4256477/
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 store html in a mysql database
提问by akano1
I'm trying to store HTML in a database, so when I retrieve the form from the database, I need to show it as a form rather than text. Is there a way to do that?
我正在尝试将 HTML 存储在数据库中,因此当我从数据库中检索表单时,我需要将其显示为表单而不是文本。有没有办法做到这一点?
This is the form
这是表格
$form = "<form id='' method='' action='' class=''>
<input type='hidden' name='my-item-id' value= $uid />
<input type='hidden' name='my-item-name' value=$title />
<input type='hidden' name='my-item-price' value='1' />
<input type='hidden' name='my-item-qty' value='1' />
<input type='submit' name='my-add-button' class='button' value='Add to cart'/>
</form>";
at the moment when I retrieve the above from the database it shows as text,
当我从数据库中检索上述内容时,它显示为文本,
also the form field in the database is varchar(1000)
数据库中的表单字段也是 varchar(1000)
include('adodb.inc.php');
include('adodb-pager.inc.php');
$sql = 'select title as "TITLE",description as "DESCRIPTION",form as "ADD TO CART" from mathspapers order by sysdate desc';
$pager = new ADODB_Pager($db,$sql);
$pager->Render();
Thanks
谢谢
回答by Dainius
When you are storing text in Database you probably use addslashes()
.
Then when you return text from Database you need something like stripslashes()
before you show your HTML text. PHP Manual on stripslashes
当您在数据库中存储文本时,您可能会使用addslashes()
.
然后,当您从数据库返回文本时,您需要stripslashes()
在显示 HTML 文本之前进行类似处理。带斜杠的 PHP 手册
回答by Erdin? ?orbac?
Instead of storing whole form pattern (and values) in the db field i can advise you to store only form field names and data (values) as key-value pairs.
我可以建议您只将表单字段名称和数据(值)存储为键值对,而不是在 db 字段中存储整个表单模式(和值)。
You can easily use them if you store them in a standard way like ; (use at form processing page)
如果您以标准方式存储它们,则可以轻松使用它们;(在表单处理页面使用)
$form_data = "my-item-id=".$_POST["my-item-id"]."&my-item-name=".$_POST["my-item-name"]."&my-item-price=".$_POST["my-item-price"]."&my-item-qty=".$_POST["my-item-qty"];
then store $form_data only at db
然后将 $form_data 仅存储在 db
to use data from db (surely after pulling $form_data from db) you can use parse_str
要使用 db 中的数据(肯定是在从 db 中提取 $form_data 之后),您可以使用 parse_str
parse_str($form_data,$form_data_from_db);
echo $form_data_from_db["my-item-id"] will print your stored form input for instance
echo $form_data_from_db["my-item-id"] 将打印您存储的表单输入例如
But these are not the my main advice. How you will build your pattern for each stored data field? Just build a function to create form like many cms does.
但这些都不是我的主要建议。您将如何为每个存储的数据字段构建您的模式?只需像许多 cms 一样构建一个函数来创建表单。
try this ;
尝试这个 ;
function myform_pattern_1($id,$method,$action,$class,$form_data){
parse_str($form_data,$fd);
$form_html = "<form id='' method='' action='' class=''>";
$form_html .= "<input type='hidden' name='my-item-id' value='".$fd["my-item-id"]."' />";
$form_html .= "<input type='hidden' name='my-item-name' value='".$fd["my-item-id"]."' />";
$form_html .= "<input type='hidden' name='my-item-price' value='".$fd["my-item-id"]."' />";
$form_html .= "<input type='hidden' name='my-item-qty' value='".$fd["my-item-id"]."' />";
$form_html .= "<input type='submit' name='my-add-button' class='button' value='Add to cart'/>";
$form_html .= "</form>";
return $form_html;
}
Call this function where you like as ;
在你喜欢的地方调用这个函数;
echo myform_pattern_1("form_id","post","","form_class",$form_data);
This usage has a great advantage to your method. You can change form syntax whenever you wish this way. You will -maybe- want to integrate a Jquery validation plugin later or just want to use another styling or even change syntax by adding tags you will have to change all stored db fields if you store whole form at db. Function usage is much more flexible.Also you will not use db for storing unnecessary -repeating- tags etc.
这种用法对您的方法有很大的好处。您可以随时以这种方式更改表单语法。您可能希望稍后集成 Jquery 验证插件,或者只想使用另一种样式,甚至通过添加标签来更改语法,如果您将整个表单存储在 db 中,您将不得不更改所有存储的 db 字段。函数使用更加灵活。此外,您不会使用 db 来存储不必要的重复标签等。
i hope you like , cys
我希望你喜欢,cys
For more information on parse_str ; http://php.net/manual/en/function.parse-str.phpenter code here
有关 parse_str 的更多信息;http://php.net/manual/en/function.parse-str.phpenter code here
回答by Mike Alberga
From the PHP documentation for htmlspecialchars: Certain characters have special significance in HTML translations made are those most useful for everyday web programming. If you require all HTML character entities to be translated, use htmlentities() instead.
来自 htmlspecialchars 的 PHP 文档:某些字符在 HTML 翻译中具有特殊意义,这些字符对日常 Web 编程最有用。如果您需要翻译所有 HTML 字符实体,请改用 htmlentities()。
The result is stored in the db and then when it is pulled from the db.
结果存储在数据库中,然后从数据库中提取。
You have it.
你拥有了它。
回答by Pekka
If I had to guess, I would say your VARCHAR
field is too small to take up all the HTML, so a tag gets cut off somewhere, resulting in the HTML being shown as text.
如果我不得不猜测,我会说您的VARCHAR
字段太小而无法容纳所有 HTML,因此标签在某处被切断,导致 HTML 显示为文本。
回答by Jakub
My guess without any more supporting evidence is that you are converting the form html into their html safe equivalents, hence it just 'outputs' as text, it is just text.
我的猜测没有任何更多支持证据是您正在将表单 html 转换为它们的 html 安全等效项,因此它只是作为文本“输出”,它只是文本。
Double check your output that the actual source code is not something like <
for <
仔细检查您的输出,实际源代码与<
<