Store HTML code in the database?

Asked

Viewed 770 times

0

I’m developing a script retrieving codes HTML from a form, and then store in the database. However I would like to store the code in the base as it was inserted into the form, with the tags, style, classes, etc...

OBJECTIVE :

  • For example, if I store the code

    <div style='color:red;'>Meu texto</div>

When reading this information (with PHP for example : echo $informacoes_da_base) would like to get as a result "My text" in red.

PROBLEM :

  • Tags are being interpreted as soon as they arrive at the base. Ex. : &lqt;code &lql;

  • When reading the information in base ex.: echo $informacoes_da_base I get on the screen unwanted characters.

I did some research on mysql_escape_string and html_entity_decode but I cannot achieve the desired results.

echo html_entity_decode($infos_da_base);

echo mysql_escape_string($infos_da_base);

CODE :

if(isset($_POST['enregistrer'])){

  Configuration::updateValue('DESCRIPTION_TIME_DELIVERY', $_POST['description']);

}
<form>
   <textarea style="width: 200px;">
       <code style='color: red;'>codigo html aqui</code>
   </textarea>
</form>
  • How are you doing? Put the code.

  • Are you running the data through a function before entering it into the database? What is it? Can you tell?

  • This must not be true: "The tags are being interpreted as soon as they arrive at the base. E.g. &lqt;code &lql;" - if it is being converted, it is before the base.

  • 1

    Put the code snippet that captures the form, and saves it in DB, that the community will probably find the problem there. Only with the code you posted, the problem cannot be reproduced.

1 answer

1


Use htmlentities() to encode this character and then the tags will look like this: &lt;/img&gt; this coding is: <img/>, That’s just saving it in the comic book. At the time of returning this data if you are going to throw them directly into the DOM then you do not need to pass function to decodes because the browser already does this, now if you are going to put it inside some input or textarea pass before in function html_entity_decode() to return the original characters.

Summary:

<?php
$a = htmlentities('<img/>'); //Retorno "&lt;/img&gt;"
$b = html_entity_decode($a); //Retorno "<img/>"
  • 4

    That’s gambiarra in my view, you don’t need to encode to save in DB. The Decode can be used to display as code, but does not need (and should not) encode anything in the DB for no reason. The author has a saving problem, and this part was not posted in the question.

  • I didn’t quite understand the question either, but I tried to make it clear how the functions work. mysql_escape_string and html_entity_decodeI believe he’s wrong in logic to return the values.

  • Yes, as far as possible you posted what you could, and the author accepted the answer. I just told you so that anyone who reads it understands that probably the real solution to the problem comes before that. The lack of details of the question prevents full analysis.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.