Reputation: 323
I am using this code to update values in a database:
function text_save( $page_name, $page_title, $page_text ) {
$servername = "127.0.0.1";
$username = "abcd";
$password = "password";
$dbname = "thedatabase";
// Create connection
$conn = new mysqli( $servername, $username, $password, $dbname );
// Check connection
if ( $conn->connect_error ) {
die( "Connection failed: " . $conn->connect_error );
}
$page_content = $page_text;
$sql = "UPDATE text SET page_title=?, page_text=? WHERE text_name=?";
// prepare and bind
$stmt = $conn->prepare( $sql );
// check whether the prepare() succeeded
if ( $stmt === false ) {
trigger_error( $this->mysqli->error, E_USER_ERROR );
}
$stmt->bind_param( 'sss', $page_title, $page_content, $page_name );
$stmt->execute();
print '<p>Text saved</p>';
$stmt->close();
$conn->close();
}
The variable $page_text
is set from a tinymce text area on a form submission and is HTML content.
If I var_dump the value of $page_text
, it comes out as:
string(23) "<p>test</p>"
When the $page_content
data is saved to the database, it is saved as:
<p>test</p>
However, if I manually set the value of $page_content
to
<p>test</p>,
e.g. $page_content = "<p>test</p>";
instead of
$page_content = $page_text;
it is saved to the database as:
<p>test</p>
I need the HTML to be saved to the database without being converted to HTML entities i.e. as <p>test</p>
not <p>test</p>
?
This is what I have tried so far:
Setting the page to utf8 - <meta charset="utf-8" />
,
setting the form to utf8 - accept-charset="UTF-8"
,
setting the connection to utf8 - mysqli_set_charset($conn,"utf8");
setting the tinymce init with - entity_encoding : "raw"
What am I doing wrong here, and why does the HTML string save correctly when I manually set the variable rather than using the form variable value (which seems to be identical)?
Thanks very much!
Upvotes: 2
Views: 2047
Reputation: 323
You might be looking for the html_entity_decode function,
http://php.net/manual/en/function.html-entity-decode.php
$page_content = html_entity_decode($page_text);
Be careful about injections and such.
Upvotes: 2
Reputation: 529
I think the problem is in the editor.
can you try to print the value of $page_content before insert into database and show us?
too, see this post. HTML Tags stripped using tinyMCE
Upvotes: 2