2015-01-03 51 views
0

我從「PHP和MongoDB Web開發」一書學習網絡開發和他們給這個代碼段用於創建一個博客帖子創建者。PHP腳本創建的網頁無法正常工作

<?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使這行得通?

+2

你有XAMPP或安裝anoother服務器和運行?把它放在php文件的頂部<?php error_reporting(E_ALL); ?> – Billy

+0

我使用Apache服務器.. – Teja713

回答

0

你的陣列的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() 
); 
+1

謝謝!有用 ! :) – Teja713

+0

不客氣Teja713 :) –