oshirowanen
oshirowanen

Reputation: 15965

Inserting HTML into database

I am inserting data into a table which contains some basic html tags, double quotes and single quotes.

I am using the following line to handle the data:

htmlentities(($_POST[content]), ENT_QUOTES);

The problem with this is that when I select this data to bring it back onto the screen, displays the actual html tags instead of rendering the html, i.e. if I use the <b>bold</b> tag, is displays it as text instead of making the text within that tag bold. If I don't use the above line, i.e.

htmlentities(($_POST[content]), ENT_QUOTES);

Then I can't insert the data into the database because the data can contain single quotes and double quotes.

How do I deal with this issue?

So basically, I should be able to insert the data into the database where single or double quotes should not cause a problem. When when rendering the data back onto the screen, it should render html tabs as they should get rendered into the browser and the quotes should be displayed as quotes in the text being rended back onto the screen.

Upvotes: 1

Views: 8458

Answers (4)

zulqer
zulqer

Reputation: 1

you have to use strip_tags($str); if you want remove only html tags.. single quote or double quote will remain... but the problem in your case is ...you are putting lots of white space with your strings so you can perfectly use use strip_tags($str);

Upvotes: 0

Sinan Eldem
Sinan Eldem

Reputation: 5738

Putting so much HTML codes into the mysql table seems an ugly method to me, it is needed if you are adding a post but if you are saving a page which you may repopulate you may consider another way.

this is my method doing this:

  1. Clear any html code
  2. Put useful data into array (serialize array)
  3. Save array into database
  4. Repopulate array when the page is called (unserialize array)

This saved me to put <1kb data instead of 125kb

This is a good way if you are using templating like systems.

Upvotes: -1

Quentin
Quentin

Reputation: 944546

You are inserting data into a database, not into an HTML document. Don't use htmlentities. Use whatever methods your database provides for escaping content. This should be something that uses bound parameters. Bobby-tables explains a number of different methods

Upvotes: 3

DhruvPathak
DhruvPathak

Reputation: 43265

$html = mysql_real_escape_string($html);

http://php.net/manual/en/function.mysql-real-escape-string.php

Make sure you have made a proper mysql connection mysql_connect before using this function.

Upvotes: 2

Related Questions