PHP 用于创建网页的脚本不起作用

PHP script to create web page not working

我正在从 "PHP and MongoDB Web Development" 书中学习网络开发,他们提供了这个用于创建博客 post 创作者的代码片段。

<?php
$action = (!empty($_POST['btn_submit']) &&
($_POST['btn_submit'] === 'Save')) ? 'save_article'
: 'show_form';
switch($action){
case 'save_article':
try {
$connection = new Mongo();
$database
= $connection->selectDB('myblogsite');
$collection = $database->
selectCollection('articles');
$article = array{
'title' => $_POST['title'],
'content' => $_POST['content'],
'saved_at' => new MongoDate()
};
$collection->insert($article);
} catch(MongoConnectionException $e) {
die("Failed to connect to database ".
$e->getMessage());
}
catch(MongoException $e) {
die('Failed to insert data '.$e->getMessage());
}
break;
case 'show_form':
default:
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"Page on w3.org">
<html xmlns="XHTML namespace" xml:lang="en"
lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=utf-8"/>
<link rel="stylesheet" href="style.css"/>
<title>Blog Post Creator</title>
</head>
<body>
<div id="contentarea">
<div id="innercontentarea">
<h1>Blog Post Creator</h1>
<?php if ($action === 'show_form'): ?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>"
method="post">
<h3>Title</h3>
<p>
<input type="text" name="title" id="title/">
</p>
<h3>Content</h3>
<textarea name="content" rows="20"></textarea>
<p>
<input type="submit" name="btn_submit"
value="Save"/>
</p>
</form>
<?php else: ?>
<p>
Article saved. _id:<?php echo $article['_id'];?>.
<a href="blogpost.php">
Write another one?</a>
</p>
<?php endif;?>
</div>
</div>
</body>
</html> 

将文件另存为 blogpost.php。 在文本编辑器中打开另一个新文件并在其中放入以下 ​​CSS 规则并将其另存为 style.css

body {
background-color: #e1ddd9;
font-size: 12px;
font-family: Verdana, Arial, Helvetica, SunSans-Regular,
Sans-Serif;
color:#564b47;
padding:20px;
margin:0px;
text-align: center;
}
div#contentarea {
text-align: left;
vertical-align: middle;
margin: 0px auto;
padding: 0px;
width: 550px;
background-color: #ffffff;
border: 1px #564b47;
}
div#innercontentarea{ padding: 10px 50px; }
div#innercontentarea form input[type=text]{ width: 435px; }
div#innercontentarea form textarea[name=content] { width: 435px; }

当我在本地主机上运行该脚本时,它只是显示一个空白 page.How 以使其工作?

你的数组 parenthesestry 块上不正确,因为你使用 { 而不是 ( 这是正确的:

try {
$connection = new Mongo();
$database
= $connection->selectDB('myblogsite');
$collection = $database->
selectCollection('articles');
$article = array(
'title' => $_POST['title'],
'content' => $_POST['content'],
'saved_at' => new MongoDate()
);