我最近接手了一个小型web开发项目,创建了一个在线公文包和博客。我一直在摆弄一些PHP代码来编辑网站上的博客文章,这给了我一些问题。最大的问题是当我将更改后的帖子提交到数据库时会发生什么。当我提交帖子时,我只收到一个空白屏幕,我现在不知所措。如果需要更多信息,请告诉我。提前谢谢。
我的尝试:
<?php
ob_start();
include('db.php');
if(isset($_GET['id']))
{
$id=$_GET['id'];
if(isset($_POST['update']))
{
htmlspecialchars($title);
htmlspecialchars($content);
strip_tags($title);
strip_tags($content);
$updated= mysql_query("UPDATE blog_entry SET title=$title, content=$content WHERE post_id='$id'")or die();
if($updated)
{
$msg="Successfully Updated!!";
echo $msg;
header('Location:blog.php');
}
}
} //update ends here ob_end_flush();
?>
这四行没有影响:
htmlspecialchars($title);
htmlspecialchars($content);
strip_tags($title);
strip_tags($content);
你必须将结果分配给一些东西:
$title = htmlspecialchars( $title );
$content = htmlspecialchars( $content );
$title = strip_tags( $title );
$content = strip_tags( $content );
还要注意,对于上面的代码,单引号('
)将不会正确转义。考虑使用:
$title = mysql_real_escape_string( $title );
$content = mysql_real_escape_string( $content );
然后,您必须用单引号将字段值括起来:
$updated = mysql_query( "UPDATE blog_entry SET title='$title', content='$content' WHERE post_id='$id'" ) or die();
# ↑ ↑ ↑ ↑
为了更好地控制错误,可以在die()
:中添加一些内容
$updated = mysql_query( ... ) or die( mysql_error() );
请注意:
mysql_语法在PHP 5.5.0中被弃用,在PHP 7.0.0中被删除。相反,应该使用MySQLi或PDO_MySQL扩展。
(来自PHP官方网站)