請確保您的數據庫中有一個唯一的ID列 - 將其稱爲「postid」,並將其設置爲每次新條目都會自動增量。
這樣,您將確保爲插入數據庫的每個條目都有唯一的標識符。
然後您需要檢索數據庫中的項目列表(包括postid),併爲數據庫中的每一行提供一個鏈接以顯示它。
如果在URL中選擇了postid,則顯示該帖子的信息。如果不是,則顯示一個列表:
<?
$dbh = new PDO('mysql:host=localhost;dbname=databasename;charset=utf8', 'username', 'password');
///No post id selected, display a list of everything
if(!$_GET['postid']) {
print "You're listing all the rows in the database<br/>";
$query = $dbh->prepare("SELECT `postid`,`name` FROM `table` ORDER by postid ASC");
$query->execute();
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
print "<a href=\"?post_id=".$row['postid']."\">".$row['name']."</a><br/>";
}
} else { //A post ID is selected
$postid = $_GET['postid'];
Print "Select from the database where postid = ".$postid;
$query = $dbh->prepare("SELECT `postid`, `name`, `keywords`, `description` FROM `table` WHERE `postid` = '$postid' LIMIT 1");
$query->execute();
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
$name = $row['name'];
$keywords = $row['keywords'];
$description = $row['description'];
print 'Displaying details for '.$name.': '.$keywords.', '.$description.' - create edit links here... <br/><br/>';
}
} //End post id is selected
?>
什麼是「islet」?它是由你定義的函數嗎? – 2014-01-08 03:16:11
你想做一些論壇? @SharanyaDutta,我認爲他的意思是isset() –
並讓你的代碼更好幫助我們閱讀。選擇代碼後,使用快捷鍵ctrl + K;) –