PHP基礎示例:用PHP+Mysql編寫簡易新聞管理系統,mysql新聞管理系統
實現目標:使用php和mysql操作函數實現一個新聞信息的發布、浏覽、修改和刪除操作
實現步驟:
一、創建數據庫和表
1.創建數據庫和表:newsdb
2.創建表格:news
字段:新聞id,標題,關鍵字,作者,發布時間,新聞內容
二、創建php文件編寫代碼(以下為要創建的php文件及其用途)
dbconfig.php 公共配置文件,數據庫連接配置信息
menu.php 網站公共導航欄
index.php 浏覽新聞的文件(此為首頁)
add.php 發布新聞表單頁
edit.php 編輯新聞的表單頁
action.php 執行新聞信息添加、修改、刪除等操作的動作(後台)
**********************************************************************
以下為數據庫創建語句:

![]()
1 create database newsdb;//創建數據庫語句
2 create table news(
3 id int unsigned not null auto_increment primary key,
4 title varchar(64) not null,
5 keywords varchar(64) not null,
6 author varchar(16) not null,
7 addtime int unsigned not null,
8 content text not null
9 );//創建表語句
數據庫創建語句
***********************************************************************
以下為dbconfig.php文件代碼
1 <?php
2 //公共信息配置
3 //數據庫配置信息
4 define("HOST","localhost"); //主機名
5 define("USER","root"); //賬號
6 define("PASS","root"); //密碼
7 define("DBNAME","newsdb"); //數據庫名
8 ?>
以下為menu.php文件代碼(一開始浏覽的頁面,添加新聞後以index頁面為主)
1 <h2>新聞管理系統</h2>
2 <a href="index.php">浏覽新聞</a>
3 <a href="add.php">發布新聞</a>
4 <hr width="90%"/>
以下為add.php文件代碼(增加具體代碼)

![]()
1 <html>
2 <head>
3 <title>新聞管理系統</title>
4 </head>
5 <body>
6 <center>
7 <?php include("menu.php");//導入導航欄 ?>
8
9 <h3>發布新聞</h3>
10 <form action = "action.php?action=add" method="post">
11 <table width="320" border="1">
12 <tr>
13 <td align="right">標題:</td>
14 <td><input type="text" name="title"/></td>
15 </tr>
16 <tr>
17 <td align="right">關鍵字:</td>
18 <td><input type="text" name="keywords"/></td>
19 </tr>
20 <tr>
21 <td align="right">作者:</td>
22 <td><input type="text" name="author"/></td>
23 </tr>
24 <tr>
25 <td align="right" valign="top">內容:</td>
26 <td><textarea cols="25" rows="5" name="content"></textarea></td>
27 </tr>
28 <tr>
29 <td colspan="2" align="center">
30 <input type="submit" value="添加"/>
31 <input type="reset" value="重置"/>
32
33 </td>
34 </tr>
35 </table>
36 </form>
37 </center>
38 </body>
39 </html>
add.php文件代碼
以下為action.php文件代碼(增刪改實現代碼)

![]()
1 <?php
2 //這是一個信息增、刪和改操作的處理頁面
3
4 //1.導入配置文件
5 require("dbconfig.php");
6 //2.連接MYSQL,並選擇數據庫
7 $link=@mysql_connect(HOST,USER,PASS) or die("數據庫連接失敗!");
8 mysql_select_db(DBNAME,$link);
9
10 //3.根據需要action值,來判斷所屬操作,執行對應的代碼
11 switch($_GET["action"])
12 {
13 case "add": //執行添加操作
14 //1.獲取要添加的信息,並補充其他信息
15 $title = $_POST["title"];
16 $keywords = $_POST["keywords"];
17 $author = $_POST["author"];
18 $content = $_POST["content"];
19 $addtime = time();
20 //2.座信息過濾(省略)
21 //3.拼裝添加SQL語句,並執行添加操作
22 $sql = "insert into news values(null,'{$title}','{$keywords}','{$author}','{$addtime}','{$content}')";
23 mysql_query($sql,$link);
24 //4.判斷是否成功
25 $id=mysql_insert_id($link);//獲取剛剛添加信息的自增id號值
26 if($id>0)
27 {
28 echo "<h3>新聞信息添加成功!</h3>";
29 }else
30 {
31 echo "<h3>新聞信息添加失敗!</h3>";
32 }
33 echo "<a href='javascript:window.history.back();'>返回</a> ";
34 echo "<a href='index.php'>浏覽新聞</a>";
35 break;
36 case "del": //執行刪除操作
37 //1.獲取要刪除的id號
38 $id=$_GET['id'];
39 //2.拼裝刪除sql語句,並執行刪除操作
40 $sql = "delete from news where id={$id}";
41 mysql_query($sql,$link);
42
43 //3.自動跳轉到浏覽新聞頁面
44 header("Location:index.php");
45 break;
46 case "update": //執行添加操作
47 //1.獲取要修改的信息
48 $title = $_POST['title'];
49 $keywords = $_POST['keywords'];
50 $author = $_POST['author'];
51 $content = $_POST['content'];
52 $id = $_POST['id'];
53 //2.過濾要修改的信息(省略)
54
55 //3.拼裝修改sql語句,並執行修改操作
56 $sql = "update news set title='{$title}',keywords='{$keywords}',author='{$author}',content='{$content}' where id = {$id} ";
57
58 mysql_query($sql,$link);
59 //4.跳轉回浏覽界面
60 header("Location:index.php");
61 break;
62 }
63 //4.關閉數據庫連接
64 mysql_close($link);
65
action.php文件代碼
以下為index.php文件代碼(在此頁面浏覽新聞,並對新聞信息進行增刪改操作)

![]()
1 <html>
2 <head>
3 <title>新聞管理系統</title>
4 <script type="text/javascript">
5 function dodel(id)
6 {
7 if(confirm("確定要刪除嗎"))
8 {
9 window.location="action.php?action=del&id="+id;
10 }
11 }
12 </script>
13 </head>
14 <body>
15 <center>
16 <?php include("menu.php");//導入導航欄 ?>
17
18 <h3>浏覽新聞</h3>
19 <table width="800" border="1">
20 <tr>
21 <th>新聞id</th>
22 <th>新聞標題</th>
23 <th>關鍵字</th>
24 <th>作者</th>
25 <th>發布時間</th>
26 <th>新聞內容</th>
27 <th>操作</th>
28 </tr>
29 <?php
30 //1.導入配置文件
31 require("dbconfig.php");
32 //2.連接MYSQL,選擇數據庫
33 $link = @mysql_connect(HOST,USER,PASS) or die("數據庫連接失敗!");
34 mysql_select_db(DBNAME,$link);
35 //3.執行查詢,並返回結果集
36 $sql = "select * from news order by addtime desc";
37 $result = mysql_query($sql,$link);
38
39 //4.解析結果集,並遍歷
40 while($row = mysql_fetch_assoc($result))
41 {
42 echo "<tr>";
43 echo "<td>{$row['id']}</td>";
44 echo "<td>{$row['title']}</td>";
45 echo "<td>{$row['keywords']}</td>";
46 echo "<td>{$row['author']}</td>";
47 echo "<td>".date("Y-m-d",$row['addtime'])."</td>";
48 echo "<td>{$row['content']}</td>";
49 echo "<td>
50 <a href='javascript:dodel({$row['id']})'>刪除</a>
51 <a href='edit.php?id={$row['id']}'>修改</a></td>";
52 echo "</tr>";
53 }
54
55 //5.釋放結果集
56 mysql_free_result($result);
57 mysql_close($link);
58 ?>
59 </table>
60 </center>
61 </body>
62 </html>
index.php文件代碼
以下為edit.php文件代碼(編輯具體代碼)

![]()
1 <html>
2 <head>
3 <title>新聞管理系統</title>
4 </head>
5 <body>
6 <center>
7 <?php
8 include("menu.php");//導入導航欄
9
10 //1.導入配置文件
11 require("dbconfig.php");
12
13 //2.連接MYSQL數據庫、選擇數據庫
14 $link = @mysql_connect(HOST,USER,PASS) or die("數據庫連接失敗!");
15 mysql_select_db(DBNAME,$link);
16 //3.獲取要修改信息的id號,並拼裝查看sql語句,執行查詢,獲取要修改的信息
17 $sql = "select *from news where id={$_GET['id']}";
18 $result = mysql_query($sql,$link);
19 //4.判斷是否獲取到了要修改的信息
20 if($result &&mysql_num_rows($result)>0)
21 {
22 $news = mysql_fetch_assoc($result);
23 }else
24 {
25 die("沒有找到要修改的信息!");
26 }
27
28 ?>
29
30 <h3>編輯新聞</h3>
31 <form action = "action.php?action=update" method="post">
32 <input type="hidden" name="id" value="<?php echo $news['id']; ?>" />
33 <table width="320" border="1">
34 <tr>
35 <td align="right">標題:</td>
36 <td><input type="text" name="title" value="<?php echo $news['title']; ?>" /></td>
37 </tr>
38 <tr>
39 <td align="right">關鍵字:</td>
40 <td><input type="text" name="keywords" value="<?php echo $news['keywords']; ?>" /></td>
41 </tr>
42 <tr>
43 <td align="right">作者:</td>
44 <td><input type="text" name="author" value="<?php echo $news['author']; ?>" /></td>
45 </tr>
46 <tr>
47 <td align="right" valign="top">內容:</td>
48 <td><textarea cols="25" rows="5" name="content"><?php echo $news['content']; ?></textarea></td>
49 </tr>
50 <tr>
51 <td colspan="2" align="center">
52 <input type="submit" value="編輯"/>
53 <input type="reset" value="重置"/>
54
55 </td>
56 </tr>
57 </table>
58 </form>
59 </center>
60 </body>
61 </html>
edit.php文件代碼