HTML 个标签作为 HTML 个实体保存到数据库
HTML tags saved to database as HTML entities
我正在使用此代码更新数据库中的值:
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();
}
变量 $page_text
是从表单提交上的 tinymce 文本区域设置的,是 HTML 内容。
如果我 var_dump $page_text
的值,结果如下:
string(23) "<p>test</p>"
$page_content
数据保存到数据库时,保存为:
<p>test</p>
但是,如果我手动将 $page_content
的值设置为
<p>test</p>,
例如$page_content = "<p>test</p>";
而不是
$page_content = $page_text;
保存到数据库中为:
<p>test</p>
我需要将 HTML 保存到数据库而不转换为 HTML 实体,即 <p>test</p>
而不是 <p>test</p>
?
这是我目前尝试过的:
正在将页面设置为 utf8 - <meta charset="utf-8" />
,
将表单设置为 utf8 - accept-charset="UTF-8"
,
将连接设置为 utf8 - mysqli_set_charset($conn,"utf8");
设置 tinymce 初始化 - entity_encoding : "raw"
我这里做错了什么,为什么当我手动设置变量而不是使用表单变量值(看起来相同)时 HTML 字符串保存正确?
非常感谢!
我认为问题出在编辑器上。
你能尝试在插入数据库之前打印 $page_content 的值并展示给我们看吗?
同样,请参阅此 post。
HTML Tags stripped using tinyMCE
您可能正在寻找 html_entity_decode 函数,
http://php.net/manual/en/function.html-entity-decode.php
$page_content = html_entity_decode($page_text);
注意注射等。
我正在使用此代码更新数据库中的值:
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();
}
变量 $page_text
是从表单提交上的 tinymce 文本区域设置的,是 HTML 内容。
如果我 var_dump $page_text
的值,结果如下:
string(23) "<p>test</p>"
$page_content
数据保存到数据库时,保存为:
<p>test</p>
但是,如果我手动将 $page_content
的值设置为
<p>test</p>,
例如$page_content = "<p>test</p>";
而不是
$page_content = $page_text;
保存到数据库中为:
<p>test</p>
我需要将 HTML 保存到数据库而不转换为 HTML 实体,即 <p>test</p>
而不是 <p>test</p>
?
这是我目前尝试过的:
正在将页面设置为 utf8 - <meta charset="utf-8" />
,
将表单设置为 utf8 - accept-charset="UTF-8"
,
将连接设置为 utf8 - mysqli_set_charset($conn,"utf8");
设置 tinymce 初始化 - entity_encoding : "raw"
我这里做错了什么,为什么当我手动设置变量而不是使用表单变量值(看起来相同)时 HTML 字符串保存正确?
非常感谢!
我认为问题出在编辑器上。
你能尝试在插入数据库之前打印 $page_content 的值并展示给我们看吗?
同样,请参阅此 post。 HTML Tags stripped using tinyMCE
您可能正在寻找 html_entity_decode 函数,
http://php.net/manual/en/function.html-entity-decode.php
$page_content = html_entity_decode($page_text);
注意注射等。