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

时间:2022-10-12 21:16:02

I am learning web development from "PHP and MongoDB Web Development" book and they have given this code snippet for creating a blog post creator.

我正在从“PHP和MongoDB Web开发”一书中学习Web开发,他们已经为创建博客文章创建者提供了此代码片段。

<?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> 

Save the file as blogpost.php . Open another new file in your text editor and put the following CSS rules in it and save it as style.css

将文件另存为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; }

When I run that script in localhost,it is just showing a blank page.How to make it work?

当我在localhost中运行该脚本时,它只是显示一个空白页面。如何使它工作?

1 个解决方案

#1


0  

Your array's parentheses is incorrect on try block because you use { instead of ( here is the correct one :

在try块上你的数组的括号是不正确的,因为你使用{而不是(这里是正确的:

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

#1


0  

Your array's parentheses is incorrect on try block because you use { instead of ( here is the correct one :

在try块上你的数组的括号是不正确的,因为你使用{而不是(这里是正确的:

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