我正在編寫我的第一個部落格.在各種教程和其他論壇的幫助下,我設法收集了半工作代碼.
現在我有一個代碼,它接受并顯示評論,但問題是協調哪些評論釋出在哪個文章上.我目前的設定是我的所有文章都是HTML檔案,評論存儲在資料庫中.我還有一個表單,用于為每個文章建立一個具有唯一文章ID和标題的新行.
我現在的基本資料庫設定如下:1個資料庫,2個表.郵政表和評論表.在評論表中,我有一般名稱,網站,評論等,我還有一個唯一的ID,可以自動增加每個評論.然後我有一個post_id應該與指定的文章比對.
在post表上,我隻有兩個字段:entry_id和title.标題由我手動設定,entry_id自動遞增.注意:條目本身不存儲在資料庫中.
是以我目前的問題是如何為每個評論頁面設定post_id以及如何将entry_id與實際文章相關聯.我希望這不會太混亂.非常感謝您的幫助!
-iMaster
解決方法:
我認為您應該考慮重構代碼以将文章存儲在資料庫中.
從那裡,你有一個頁面(http://mysite/showpost.php?post_id = 5)顯示你的文章(psuedo-code’ish):
// establish database connection here
// Simple SQL injection prevention:
foreach ($_REQUEST as $key => $value)
{
$_REQUEST[$key] = mysql_real_escape_string($value);
}
// Get the appropriate post from the posts table.
$query = "SELECT post FROM posts WHERE post_id={$_REQUEST['post_id']}";
$result = mysql_query($query);
$row = mysql_fetch_assoc($query);
echo $row['posts'];
// Get the appropriate comments from the comments table.
$query = "SELECT comment FROM comments WHERE post_id={$_REQUEST['post_id']}";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
echo "Comment: {$row['comment']}";
}
// close connections, etc.
?>
我的PHP非常生疏,但這應該讓您對完成所需内容所需的資料結構和代碼有所了解.
标簽:php,mysql,database,comments,blogs
來源: https://codeday.me/bug/20190610/1214231.html