修改数据的PHP问题

时间:2012-06-17 18:22:14

标签: php

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

< h2>关于Page< / 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();
?>

1 个答案:

答案 0 :(得分:1)

不要替换插入内容。替换显示的字符是 显示 问题,应该在 上完成显示