-2

将数据写入我的数据库时遇到问题。当我编辑页面并使用 HTML 标记插入数据时,它会损坏。例如,一些由 < h2 > 标签包围的文本,当我检查数据库时,我看到了额外的字符 <h2>....

<h2>关于页面</h2>

<p>这是关于页面</p>

然后,当我重新加载我的网页时,我看到了我输入的文本和标签,显然我不想看到它们。任何想法为什么会发生这种情况?

<?php 
session_start();
include_once "admin_check.php";
?>
<?php 
first thing, I did not do that
$pid = preg_replace('#[^0-9]#', '', $_POST['pid']); // filter everything but numbers     for security

include_once "../scripts/connect_to_mysql.php";
$sqlCommand = "SELECT pagetitle, linklabel, pageorder, pagebody FROM pages WHERE id='$pid' LIMIT 1"; 
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error()); 
while ($row = mysqli_fetch_array($query)) { 
    $pagetitle = $row["pagetitle"];
    $linklabel = $row["linklabel"];
    $pageorder = $row["pageorder"];
    $pagebody = $row["pagebody"];
    $pagebody = str_replace("<br />", "", $pagebody);
    $pagebody = nl2br(htmlspecialchars($pagebody));
} 
mysqli_free_result($query); 
?>

<?php
$pid = $_POST['pid'];
$pagetitle = $_POST['pagetitle'];
$linklabel = $_POST['linklabel'];
$pagebody = $_POST['pagebody'];
// Filter Function -------------------------------------------------------------------
function filterFunction ($var) { 
    $var = nl2br(htmlspecialchars($var));
    $var = str_replace("'", "&#39;", $var);
    $var = str_replace("`", "&#39;", $var);     
    return $var; 
} 
$pagetitle = filterFunction($pagetitle);
$linklabel = filterFunction($linklabel);
$pagebody = filterFunction($pagebody);
// End Filter Function --------------------------------------------------------------
include_once "../scripts/connect_to_mysql.php";
// Add the updated info into the database table
$query = mysqli_query($myConnection, "UPDATE pages SET pagetitle='$pagetitle', linklabel='$linklabel', pagebody='$pagebody', lastmodified='now()' WHERE id='$pid'") or die (mysqli_error($myConnection));

echo 'Operation Completed Successfully! <br /><br /><a href="index.php">Click Here</a>';
exit();
?>
4

1 回答 1

1

不要在插入时更换。更换显示字符是显示问题,应在显示时进行。

于 2012-06-17T18:26:17.687 回答