Skip to content
Advertisement

How to Save HTML to mysql using PHP?

I am trying to post some html though a form to a php page and then trying to save that html into database the problem which I am facing is that when I save this to database it includes many html entities with it

for example my original html code is

<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>

But When I save it db it becomes

    <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>

what should I do to save the html as it is and echo it any time it is needed as a html code

here is my PHP code

    $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);

Advertisement

Answer

You could use htmlspecialchars and htmlspecialchars_decode combined with htmlEntities ,html_entity_decode

htmlspecialchars — Convert special characters to HTML entities

See documentation here http://php.net/htmlspecialchars

htmlspecialchars_decode — Convert special HTML entities back to characters
See documentation here http://php.net/manual/en/function.htmlspecialchars-decode.php

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

echo html_entity_decode(htmlspecialchars_decode($htmlcode));
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement