首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP+MySQL 簡易新聞發布系統實作完整代碼

     編輯:PHP 綜合知識

包含新聞發布、列表分頁、詳細頁面三大模組,採用共用配置檔簡化維護。

共用配置 (common_news.php)

<?php
$id = mysql_connect('localhost','root','cgreen');
$db = mysql_select_db('db_news', $id);
$page_size = 5; // 每頁筆數
?>

發布頁面 (pub.php)

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

發布處理 (pub_ok.php)

<?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.php)

  • JavaScript view() 彈出視窗顯示詳細內容。
  • PHP 計算總筆數、總頁數、偏移量 $offset。
  • LIMIT $offset, $page_size 實現分頁查詢。
  • 迴圈輸出標題連結,連結至 view_d.php?recid=...。
  • 分頁導航:首頁、上一頁、下一頁、尾頁,邊界判斷完善。

關鍵技術點

  1. htmlspecialchars() 防 XSS。
  2. str_replace("\n", "<br>") 保留換行顯示。
  3. 共用配置檔集中管理連線參數。
  4. 分頁邏輯清晰,易擴充為類別。

此範例展示了典型 CMS 核心流程,可作為進階開發藍本。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved