Skip to content Skip to sidebar Skip to footer

How To Insert Html Into Database Using Php

I have the following html code:

Solution 1:

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"