如何在数据库中插入和检索“html代码”?

Can anyone tell me how do I properly take special characters as input from a textbox, and insert it into database and retrieve it and display it on another textbox. For example, how do I deal with content like this, <html><body><anything>" " ' its has html tag, special chars and also some random text which looks like html tag, but is not. Or is there any way to deal with it in codeigniter?

My code example are.

<input type="text" id="videoTitle" name="title" value="<?php echo htmlentities($videoToEdit['title']); ?>> 

to take input from user. It is also echoing the same content in the input field.

$data['title'] = htmlentities($this->input->post('title'));

to store the user input.

<strong><?php echo html_entity_decode($video['title']); ?></strong>

to display the content in some div. And doing this, the value stored in the database is &amp;lt;body&amp;gt; &lt;Some&gt; title &lt;l&gt;&quot;m&quot; and the output I get is <body> title "m". The solution I'm looking for is similar to what stackoverflow does. Like, we can input whatever character we need and it is displayed exactly as it is in the comments below.

You can do something like htmlentities for encoding and saving it to DB:

htmlentities($textareaContents);      // ==> Send to DB

And while retrieving, you can make use of html_entity_decode:

echo html_entity_decode($textFromDB); // ==> Display on HTML

In short, the above functions do these:

htmlentities("<html>");             // ==> "&lt;html&gt;"
html_entity_decode("&lt;html&gt;"); // ==> "<html>"