Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following html code:

<div class=" modal" id="indexModal">
    <span class="thing"></span>
    <a href="#" class="closeMe" title="Inchide">&#238;nchide</a>
    <h2 class="title">Promo&#355;ia lunii februarie!</h2>
    <div>
        <h3>Beneficia&#355;i de promo&#355;ia sezonului.</h3>
        <p>&#206;n luna Februarie,  v&#259; ofer&#259; posibilitatea de a achizi&#355;ioana, solarii profesionale  &#351;i hobby, sisteme de irigatii, folie &#351;i accesorii  cu o reducere de 10% aplicat&#259; la pre&#355;ul de producator.</p>
        <a href="http://www.domain.com/solarii/10/solarii-profesionale-latimi-6m-sau-8m" class="orangeButton">vede&#355;i oferta special&#259;!</a>
    </div>
</div>

and I want to insert it into database but it doesn't saves the full content. I tried using htmlentities() on insert, and html_entity_decode() when I want to display the html. But all I get is this:

<div class=" modal" id="indexModal">
    <span class="thing"></span>
    <a href="#" class="closeMe" title="Inchide">?nchide</a>
    <

Can you explain me, how to safely insert some html in database without problems and how to display that html afterwards on a page? My table is utf8_general_ci charset.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
169 views
Welcome To Ask or Share your Answers For Others

1 Answer

The short answer is 'don't put HTML into a database'.

There are a lot of very good reasons for this. Some are:

  • Reusing the data in another non-HTML context is not possible
  • Extracting the data with other data joined to it is not possible
  • You end up with encoding problems like you are having now
  • If you wish to change the way your site is laid out, you have to update every database field, rather than just one HTML template.

Use PHP to extract and store just text, or just numbers and store them in a proper set of relational tables. If you are not sure how, take the time to learn, otherwise you will find many more headaches further down the line when you inevitably want to expand the site to other things you haven't thought of yet, or change the way it works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...