网站的PHP注释脚本不起作用

网站的PHP注释脚本不起作用,php,mongodb,web,webpage,Php,Mongodb,Web,Webpage,我从“PHP和MongoDB web开发”一书中学习web开发。我遇到了创建博客和添加评论的说明 1)在文本编辑器中打开blog.php,并用 以下内容: <?php $id = $_GET['id']; try { $connection = new Mongo(); $database = $connection->selectDB('myblogsite'); $collection = $database->selectCollection('articles'); }

我从“PHP和MongoDB web开发”一书中学习web开发。我遇到了创建博客和添加评论的说明

1)在文本编辑器中打开blog.php,并用 以下内容:

<?php
$id = $_GET['id'];
try {
$connection = new Mongo();
$database
= $connection->selectDB('myblogsite');
$collection = $database->selectCollection('articles');
} catch(MongoConnectionException $e) {
die("Failed to connect to database ".$e->getMessage());
}
$article = $collection->findOne(array('_id' =>
new MongoId($id)));
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8"/>
<link rel="stylesheet" href="style.css" />
<title>My Blog Site</title>
</head>
<body>
<div id="contentarea">
<div id="innercontentarea">
<h1><?php echo $article['title']; ?></h1>
<p><?php echo $article['content']; ?></p>
<div id="comment-section">
<h3>Comments</h3>
<?php if (!empty($article['comments'])): ?>
<h3>Comments</h3>
<?php foreach($article['comments'] as $comment):echo $comment['name'].' says...';?>
<p><?php echo $comment['comment']; ?></p>
<span>
<?php echo date('g:i a, F j', $comment['posted_at']->sec); ?>
</span><br/><br/><br/>
<?php endforeach;endif;?>
<h3>Post your comment</h3>
<form action="comment.php" method="post">
<span class="input-label">Name</span>
<input type="text" name="commenter_name" class="comment-input"/>
<br/><br/>
<span class="input-label">Email</span>
<input type="text" name="commenter_email" class="comment-input"/>
<br/><br/>
<textarea name="comment"vrows="5"></textarea><br/><br/>
<input type="hidden" name="article_id" value="<?php echo $article['_id']; ?>"/>
<input type="submit" name="btn_submit" value="Save"/>
</form>
</div>
</div>
</div>
</body>
</html>
<?php
$id = $_POST['article_id'];
try {
$mongodb = new Mongo();
$collection = $mongodb->myblogsite->articles;
} catch (MongoConnectionException $e) {
die('Failed to connect to MongoDB '.$e->getMessage());
}
$article = $collection->findOne(array('_id' => MongoId($id)));
$comment = array('name' => $_POST['commenter_name'],'email' => $_POST['commenter_email'],'comment' => $_POST['comment'],'posted_at' => new MongoDate());
$collection->update(array('_id' => new MongoId($id)),array('push' => array('comments' => $comments)));
header('Location: blogs.php?id='.$id);
?>
<?php
try {
$connection = new Mongo();
$database
= $connection->selectDB('myblogsite');
$collection = $database->selectCollection('articles');
} catch(MongoConnectionException $e) {
die("Failed to connect to database ".$e->getMessage());
}
$cursor = $collection->find();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8"/>
<link rel="stylesheet" href="style.css" />
<title>My Blog Site</title>
</head>
<body>
<div id="contentarea">
<div id="innercontentarea">
<h1>My Blogs</h1>
<?php while ($cursor->hasNext()):
$article = $cursor->getNext(); ?>
<h2><?php echo $article['title']; ?></h2>
<p>
<?php echo substr($article['content'], 0,
200).'...'; ?>
</p>
<a href="blog.php?id=<?php echo $article['_id'];
?>">Read more</a>
<?php endwhile; ?>
</div>
</div>
</body>
</html>

我的博客网站

评论 评论




发表你的评论 名称

电子邮件



3)在浏览器中导航到blogs.php,单击顶部的“阅读更多”链接 文章将在blog.php页面中阅读其全部内容。blogs.php的代码如下:

<?php
$id = $_GET['id'];
try {
$connection = new Mongo();
$database
= $connection->selectDB('myblogsite');
$collection = $database->selectCollection('articles');
} catch(MongoConnectionException $e) {
die("Failed to connect to database ".$e->getMessage());
}
$article = $collection->findOne(array('_id' =>
new MongoId($id)));
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8"/>
<link rel="stylesheet" href="style.css" />
<title>My Blog Site</title>
</head>
<body>
<div id="contentarea">
<div id="innercontentarea">
<h1><?php echo $article['title']; ?></h1>
<p><?php echo $article['content']; ?></p>
<div id="comment-section">
<h3>Comments</h3>
<?php if (!empty($article['comments'])): ?>
<h3>Comments</h3>
<?php foreach($article['comments'] as $comment):echo $comment['name'].' says...';?>
<p><?php echo $comment['comment']; ?></p>
<span>
<?php echo date('g:i a, F j', $comment['posted_at']->sec); ?>
</span><br/><br/><br/>
<?php endforeach;endif;?>
<h3>Post your comment</h3>
<form action="comment.php" method="post">
<span class="input-label">Name</span>
<input type="text" name="commenter_name" class="comment-input"/>
<br/><br/>
<span class="input-label">Email</span>
<input type="text" name="commenter_email" class="comment-input"/>
<br/><br/>
<textarea name="comment"vrows="5"></textarea><br/><br/>
<input type="hidden" name="article_id" value="<?php echo $article['_id']; ?>"/>
<input type="submit" name="btn_submit" value="Save"/>
</form>
</div>
</div>
</div>
</body>
</html>
<?php
$id = $_POST['article_id'];
try {
$mongodb = new Mongo();
$collection = $mongodb->myblogsite->articles;
} catch (MongoConnectionException $e) {
die('Failed to connect to MongoDB '.$e->getMessage());
}
$article = $collection->findOne(array('_id' => MongoId($id)));
$comment = array('name' => $_POST['commenter_name'],'email' => $_POST['commenter_email'],'comment' => $_POST['comment'],'posted_at' => new MongoDate());
$collection->update(array('_id' => new MongoId($id)),array('push' => array('comments' => $comments)));
header('Location: blogs.php?id='.$id);
?>
<?php
try {
$connection = new Mongo();
$database
= $connection->selectDB('myblogsite');
$collection = $database->selectCollection('articles');
} catch(MongoConnectionException $e) {
die("Failed to connect to database ".$e->getMessage());
}
$cursor = $collection->find();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"
lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html" charset="utf-8"/>
<link rel="stylesheet" href="style.css" />
<title>My Blog Site</title>
</head>
<body>
<div id="contentarea">
<div id="innercontentarea">
<h1>My Blogs</h1>
<?php while ($cursor->hasNext()):
$article = $cursor->getNext(); ?>
<h2><?php echo $article['title']; ?></h2>
<p>
<?php echo substr($article['content'], 0,
200).'...'; ?>
</p>
<a href="blog.php?id=<?php echo $article['_id'];
?>">Read more</a>
<?php endwhile; ?>
</div>
</div>
</body>
</html>

我的博客网站
我的部落格


当我试图发表评论时,它会重定向到空白页。如何使这段代码工作?

首先,在comment.php中的所有代码中添加以下内容(就在php开始标记下):


这将显示导致空白页的错误,希望能帮助您。

检查PHP日志是否有错误。如果关闭,则打开PHP错误报告。空白页面通常意味着存在某种错误,但您需要找出错误的原因和位置。这些错误是:已弃用:main():Mongo类已弃用,请在第6行使用/var/www/html/comment.php中的MongoClient类致命错误:调用未定义的函数MongoId()在第11行的/var/www/html/comment.php中,我试图修复它们,但失败了。你能帮我吗?我自己从来没有使用过Mongo,但这表明你使用的评论脚本已经过时。你可以尝试更改$connection=new Mongo();to$connection=new MongoClient();不过只是猜测而已