我创建了一个CMS,并在我的博客文章中编写了以下代码来显示代码。
<pre>
<code>
<input type="color" name="favcolor" />
</code>
</pre>
它显示
<input type="color" name="favcolor" />
在我的博客文章页面上。但是回到我的编辑页面,一旦保存并刷新,代码就会变成这样:
<pre>
<code>
<input type="color" name="favcolor" />
</code>
</pre>
如何显示我编写的代码?据我所知,代码按原样保存在mySQL数据库上。所以我猜我在获取内容时做错了什么..
这是我用来在编辑页面上显示内容的功能。
function get_post($post_id){
include('includes/database.php');
$sql = 'SELECT post_id, post_content FROM blog_posts WHERE post_id = ?';
try {
$post_stmt = $pdo->prepare($sql);
$post_stmt->bindValue(1,$post_id,PDO::PARAM_INT);
$post_stmt->execute();
return $post_stmt->fetch();
} catch (Exception $e) {
echo 'Error:'.$e->getMessage().'</ br>';
return array();
}
}
在编辑帖子中.php
$row = get_post($post_id);
<textarea name='post_content' id="post_content">
<?php echo $row['post_content'];?>
</textarea>
综上所述,问题:转义的 HTML 在保存在编辑页面上时变为未转义。问:如何将转义的 HTML 保留在编辑页面上?
像这样...
$row = get_post($post_id);
<textarea name='post_content' id="post_content">
<?php echo htmlentities ( $row['post_content'], ENT_QUOTES, 'UTF-8' );?>
</textarea>