包含新聞發布、列表分頁、詳細頁面三大模組,採用共用配置檔簡化維護。
<?php
$id = mysql_connect('localhost','root','cgreen');
$db = mysql_select_db('db_news', $id);
$page_size = 5; // 每頁筆數
?>
<form name="form1" method="post" action="pub_ok.php" onsubmit="return Juge(this);">
<table width="90%" align="center">
<tr><td>標題:</td></tr>
<tr><td><input type="text" name="title" size="80"></td></tr>
<tr><td>詳細內容:</td></tr>
<tr><td><textarea name="detail" style="width:80%;height:160px;"></textarea></td></tr>
<tr><td><input type="submit" name="pub" value="發布"></td></tr>
</table>
</form>
<?php include "common_news.php"; ?>
<html><head><title>新聞發布</title></head><body><?php
if ($pub) {
$title = htmlspecialchars($title);
$detail = htmlspecialchars($detail);
$detail = str_replace("\n", "<br>", $detail);
$pub_time = date("Y-m-d H:i:s");
$query = "INSERT INTO news (title, detail, pub_time) VALUES ('$title','$detail','$pub_time')";
$result = mysql_query($query, $db);
echo "<div align='center'>";
if ($result) {
echo "<p>發布成功!<a href='view.php?page=1'>查看</a>、<a href='pub.php'>發布</a></p>";
} else { echo mysql_error(); echo "<p>發布失敗!</p>"; }
echo "</div>";
}
?></body></html>
view() 彈出視窗顯示詳細內容。$offset。LIMIT $offset, $page_size 實現分頁查詢。view_d.php?recid=...。htmlspecialchars() 防 XSS。str_replace("\n", "<br>") 保留換行顯示。此範例展示了典型 CMS 核心流程,可作為進階開發藍本。