Nrc Nrc - 1 year ago 40
HTML Question

HTML code in a database

Is a good practice to put html code in a MySql database?. To be more specific, I have a blog. I have a separated field in the database for each part: the title of the page (metadata), the description, the title of the post, the body of the post... The field of the body sometimes needs html or a way to distinguish the subtitles, the img or the videos and their captions that are between the text of the body of each post.

I am the only one that puts content in that database. So I suppose I do not need to clean any html.

  • Is a good practice to put html code in a MySql database?

  • If html is not a good practice, how to distinguish YouTube videos and caption that are between the text of the body of the post

Answer Source

I would not suggest storing HTML in your database because it requires too many cleanups to prevent storing malicious Javascript code in your database. So instead, you can use a PHP markdown library to nicely format your messages with a human intuitive format, which will be parsed into HTML when you display it, similar to Stackoverflow or Github.



1. List 1
2. List 2

Translates to:

   <li>List 1</li>
   <li>List 1</li>

As for the Youtube embedding mentioned in the comment, there is a simple solution with markdown: Add an extra markdown rule!

You can make the markdown syntax clean like this: @[Alt text] (YouTube-code)

Which would translate to the following HTML:

<iframe class="youtube" width="560" height="315" src="{{YouTube-code }}?wmode=opaque" frameborder="0" allowfullscreen>{{ Alt text }}</iframe>

Check out the source of this information on how to implement this markdown syntax for YT: