php - 如何使用 PHP 将 HTML 保存到 mysql?

标签 php html mysql

我正在尝试通过表单将一些 html 发布到 php 页面,然后尝试将该 html 保存到数据库中 我面临的问题是,当我将其保存到数据库时,它包含许多 html 实体

例如我原来的html代码是

<div class="flexslider">
 <ul class="slides">
<li class="slide slide--text" data-text="Great Plans And Pricing To Help You Get Started With Your E-Commerce Business">
  <div class="overdiv" style="position: absolute;">
           <form method="post" action="https://www.marclick.com/customer/account/create/">
              <div>
                 <input required type="email" name="email" value="" style="border-radius:10px;" class="form-control" placeholder="Your Email">
                 <div class="text-left"></div>
                 <input type="submit" value="Get Your Online Shop Now!" class="btn btn-warning btn--decorated">
              </div>
           </form>
           <p class="text-left">No Credit Card Required</p>
 </div>
  <img src="img/home_slider/1.jpg" alt="">
</li>
</ul>

但是当我将它保存到数据库时,它就变成了

    <p>&lt;!-- Flex full width slider --&gt;<br />
&lt;div class=&quot;flexslider&quot;&gt;<br />
&nbsp;&nbsp;<br />
&nbsp; &lt;ul class=&quot;slides&quot;&gt;<br />
&nbsp; &nbsp;&nbsp; &nbsp;&lt;li class=&quot;slide slide--text&quot; data-text=&quot;Great Plans And Pricing To Help You Get Started With Your E-Commerce Business&quot;&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;div class=&quot;overdiv&quot; style=&quot;position: absolute;&quot;&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;h2&gt;Get a &lt;span style=&quot;color:#E92803;-webkit-text-stroke-width: 1px;-webkit-text-stroke-color: black&quot;&gt;FREE&lt;/span&gt; Gorgeous Shop in Minutes and Sell Anywhere.&lt;/h2&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;form method=&quot;post&quot; action=&quot;https://www.marclick.com/customer/account/create/&quot;&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;div&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;input required type=&quot;email&quot; name=&quot;email&quot; value=&quot;&quot; style=&quot;border-radius:10px;&quot; class=&quot;form-control&quot; placeholder=&quot;Your Email&quot;&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;div class=&quot;text-left&quot;&gt;&lt;/div&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;input type=&quot;submit&quot; value=&quot;Get Your Online Shop Now!&quot; class=&quot;btn btn-warning btn--decorated&quot;&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &lt;/div&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;/form&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&lt;p class=&quot;text-left&quot;&gt;No Credit Card Required&lt;/p&gt;<br />
&nbsp; &nbsp; &nbsp;&lt;/div&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;img src=&quot;img/home_slider/1.jpg&quot; alt=&quot;&quot;&gt;<br />
&nbsp; &nbsp; &lt;/li&gt;</p>

我应该如何保存 html 并在需要时将其作为 html 代码进行回显

这是我的 PHP 代码

    $content=$_POST['editor1']; //html
    $seo=addslashes($_POST['keywords']);
    $category=addslashes($_POST['category']);
    $query="INSERT INTO `pages`(`name`,`link`,`seo`,`content`, `category`)
     VALUES 
     ('$title','$permalink','$seo','$content','$category')";
    $result=mysqli_query($link, $query);

最佳答案

您可以将 htmlspecialcharshtmlspecialchars_decodehtmlEntitieshtml_entity_decode 结合使用

htmlspecialchars — 将特殊字符转换为 HTML 实体

请参阅此处的文档 http://php.net/htmlspecialchars

htmlspecialchars_decode — 将特殊 HTML 实体转换回字符
请参阅此处的文档 http://php.net/manual/en/function.htmlspecialchars-decode.php

$htmlcode = htmlentities(htmlspecialchars(thehmldata));
echo $htmlcode;

echo html_entity_decode(htmlspecialchars_decode($htmlcode));

关于php - 如何使用 PHP 将 HTML 保存到 mysql?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31264531/

相关文章:

php - 商业项目中的 CodeIgniter PHP 框架

php - 如何使用 PHPUnit 测试多个异常?

C# 到 mysql - 使用外键将新行插入到表中

javascript - 在javascript中获取总秒数

html - div 宽度不会在调整大小时更新

php 不会插入到 mysql 中。没有错误

php - Mysql更新使用限制

php - 向迁移添加外键失败 - Laravel

php - 如何使用php正则表达式

xhtml - 我看过很多浏览器的 CSS 兼容性列表,有 X/HTML 标签的列表吗?还是所有浏览器都支持所有标签?