phpmysqldatabaseblogsblog-engine

is there an efficient way to insert blog entry containing code snippets into MySQL database?


I am building a blog engine for practice. The design is supposed to be fairly simple I guess..Here is how i want it to work:

  1. I have index.html, a CSS sheet, some javascript, and one PHP file i named pullContent.php.
  2. Index.html is the main page where you see the blog posts. pullContent.php interfaces with MySQL database table containing my posts.
  3. inside the body of index.html i have a div with id="blog_entries" containing the blog posts. Inside that div i will have a bunch of divs with classes="post" that will make up the set of my blog posts. Each post will be pulled from a row on MySQL table, displayed on index.html and then after that be styled by CSS. This dirty work is done in pullContent.php

As you can see, I will have to insert each of my blog entry into the database table and the content inserted should be in the form of HTML markup (since some parts of the post probably needs to be styled using CSS and thus needs some class/id identifier). Now In the case where I want to put some code snippet on my blog, isn't it going to be such a hassle? is there any workaround using some other technology i did not mention?

any comments/suggestions would be appreciated. thx.


Solution

  • First of all you should make sure to properly sanitise your data when passing it to the database.

    When you want to display code, just put everything in a pre-block (just like you would here on SO)

    
    <pre>
    /* your code */
    </pre>
    
    

    This way your post will be displayed with all the spaces, newlines, as it was passed to you originally.

    On the other hand you still have to make sure to escape certain characters: eg. instead of <, > or & you should put &lt;, &gt; and &amp;

    If you want your code to look good, then check out google-code-prettify (some extra js & CSS and your code will look aweseome!)