How To Insert Html Into Database Using Php
Solution 1:
The short answer is 'don't put HTML into a database'.
There are a lot of very good reasons for this. Some are:
- Reusing the data in another non-HTML context is not possible
- Extracting the data with other data joined to it is not possible
- You end up with encoding problems like you are having now
- If you wish to change the way your site is laid out, you have to update every database field, rather than just one HTML template.
Use PHP to extract and store just text, or just numbers and store them in a proper set of relational tables. If you are not sure how, take the time to learn, otherwise you will find many more headaches further down the line when you inevitably want to expand the site to other things you haven't thought of yet, or change the way it works.
Solution 2:
You need to change your database structure to accept more characters. Example: Set your column type to varchar 250 (or however many characters you need) *it does have a maximum number of characters though.
Solution 3:
Im not sure what you mean by insert safely, if you mean that you get funny characters with the charset then try the following two steps
1) First you need to check what charset your mysql is setup as. Different setups default to different ones. I generally go for utf8. it should be fairly straight forward to change and setup. From mysql administrator or some other tool. see following http://dev.mysql.com/doc/refman/5.0/en/charset-connection.html
2) Then your html document headers if youadd the following meta tag
<metahttp-equiv="content-type"content="text/html;charset=UTF-8" />
this should link the two charsets up and remove funny characters
Solution 4:
Just use PDO prepare statement and it will allow any kind of text to be added on the database. It will actually consider all the variables as text, so it will add in the way you are looking for Like this:
$host = 'localhost';
$user = 'root';
$password = '';
$dbname = 'myDatabase';
$dsn = 'mysql:host='.$host.';dbname='.$dbname.';charset=utf8mb4';
$conn = new PDO($dsn, $user, $password);
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$query = "INSERT INTO yourTable(someColumn, messageHTML) VALUES (?, ?)";
$stmt = $conn->prepare($query);//This is the line that will consider all as text$stmt->execute([$something, $html]);
Post a Comment for "How To Insert Html Into Database Using Php"