1 实现一个小型的新闻管理项目,包括对新闻信息的增删改查。页面功能如图 - 1、图 - 2 所示。
图 - 1
图 - 2
参考答案
在 phpStudy 的安装目录下,找到 www 子目录,.php 文件位置要在该目录下。
1)创建 dbconfig.php 文件,功能是数据库信息的设置,内容如下:
1. <?php
4. define("HOST","localhost");
5. define("USER","root");
6. define("PASS","root");
7. define("DBNAME","newsdb");
8. ?>
2)创建 menu.php 文件,功能是导航菜单的设置,内容如下:
1. <h2>新闻管理系统</h2>
2. <a href="index.php">浏览新闻</a>
3. <a href="add.php">发布新闻</a>
4. <hr width="90%"/>
3)创建 index.php 文件,功能是新闻信息的浏览,内容如下:
1. <html>
2. <head>
3. <meta charset="UTF-8">
4. <title>新闻管理系统</title>
5. <script type="text/javascript">
6. function dodel(id)
7. {
8. if(confirm("确定要删除吗"))
9. {
10. window.location="action.php?action=del&id="+id;
11. }
12. }
13. </script>
14. </head>
15. <body>
16. <center>
17. <?php include("menu.php");
19. <h3>浏览新闻</h3>
20. <table width="800" border="1">
21. <tr>
22. <th>新闻id</th>
23. <th>新闻标题</th>
24. <th>关键字</th>
25. <th>作者</th>
26. <th>发布时间</th>
27. <th>新闻内容</th>
28. <th>操作</th>
29. </tr>
30. <?php
31. date\_default\_timezone_set("PRC");
33. require("dbconfig.php");
35. $link = @mysqli_connect(HOST,USER,PASS) or die("数据库连接失败!");
36. mysqli\_select\_db($link,DBNAME);
38. $sql = "select * from news order by addtime desc";
39. $result = mysqli_query($link,$sql);
42. while($row = mysqli\_fetch\_assoc($result))
43. {
44. echo "<tr>";
45. echo "<td>{$row\['id'\]}</td>";
46. echo "<td>{$row\['title'\]}</td>";
47. echo "<td>{$row\['keywords'\]}</td>";
48. echo "<td>{$row\['author'\]}</td>";
49. echo "<td>".date("Y-m-d",$row\['addtime'\])."</td>";
50. echo "<td>{$row\['content'\]}</td>";
51. echo "<td>
52. <a href='javascript:dodel({$row\['id'\]})'>删除</a>
53. <a href='edit.php?id={$row\['id'\]}'>修改</a></td>";
54. echo "</tr>";
55. }
58. mysqli\_free\_result($result);
59. mysqli_close($link);
60. ?>
61. </table>
62. </center>
63. </body>
64. </html>
4)创建 action.php 文件,功能是数据的增删改查的实现,内容如下:
1. <?php
3. header("Content-type: text/html; charset=utf-8");
7. require("dbconfig.php");
9. $link=@mysqli_connect(HOST,USER,PASS) or die("数据库连接失败!");
10. mysqli\_select\_db($link,DBNAME);
13. switch($_GET\["action"\])
14. {
15. case "add":
17. $title = $_POST\["title"\];
18. $keywords = $_POST\["keywords"\];
19. $author = $_POST\["author"\];
20. $content = $_POST\["content"\];
21. $addtime = time();
24. $sql = "insert into news values(null,'{$title}','{$keywords}','{$author}','{$addtime}','{$content}')";
25. mysqli_query($link,$sql);
27. $id=mysqli\_insert\_id($link);
28. if($id>0)
29. {
30. echo "<h3>新闻信息添加成功!</h3>";
31. }else
32. {
33. echo "<h3>新闻信息添加失败!</h3>";
34. }
35. echo "<a href='javascript:window.history.back();'>返回</a> ";
36. echo "<a href='index.php'>浏览新闻</a>";
37. break;
38. case "del":
40. $id=$_GET\['id'\];
42. $sql = "delete from news where id={$id}";
43. mysqli_query($link,$sql);
46. header("Location:index.php");
47. break;
48. case "update":
50. $title = $_POST\['title'\];
51. $keywords = $_POST\['keywords'\];
52. $author = $_POST\['author'\];
53. $content = $_POST\['content'\];
54. $id = $_POST\['id'\];
58. $sql = "update news set title='{$title}',keywords='{$keywords}',author='{$author}',content='{$content}' where id = {$id} ";
60. mysqli_query($link,$sql);
62. header("Location:index.php");
63. break;
64. }
66. mysqli_close($link);
5)创建 add.php 文件,功能是数据信息的设置,内容如下:
1. <html>
2. <head>
3. <meta charset="UTF-8">
4. <title>新闻管理系统</title>
5. </head>
6. <body>
7. <center>
8. <?php include("menu.php");
10. <h3>发布新闻</h3>
11. <form action = "action.php?action=add" method="post">
12. <table width="320" border="1">
13. <tr>
14. <td align="right">标题:</td>
15. <td><input type="text" name="title"/></td>
16. </tr>
17. <tr>
18. <td align="right">关键字:</td>
19. <td><input type="text" name="keywords"/></td>
20. </tr>
21. <tr>
22. <td align="right">作者:</td>
23. <td><input type="text" name="author"/></td>
24. </tr>
25. <tr>
26. <td align="right" valign="top">内容:</td>
27. <td><textarea cols="25" rows="5" name="content"></textarea></td>
28. </tr>
29. <tr>
30. <td colspan="2" align="center">
31. <input type="submit" value="添加"/>
32. <input type="reset" value="重置"/>
34. </td>
35. </tr>
36. </table>
37. </form>
38. </center>
39. </body>
40. </html>
6)创建 edit.php 文件,功能是数据信息的设置,内容如下:
1. <html>
2. <head>
3. <meta charset="UTF-8">
4. <title>新闻管理系统</title>
5. </head>
6. <body>
7. <center>
8. <?php
9. include("menu.php");
12. require("dbconfig.php");
15. $link = @mysqli_connect(HOST,USER,PASS) or die("数据库连接失败!");
16. mysqli\_select\_db($link,DBNAME);
18. $sql = "select *from news where id={$_GET\['id'\]}";
19. $result = mysqli_query($link,$sql);
21. if($result &&mysqli\_num\_rows($result)>0)
22. {
23. $news = mysqli\_fetch\_assoc($result);
24. }else
25. {
26. die("没有找到要修改的信息!");
27. }
29. ?>
31. <h3>编辑新闻</h3>
32. <form action = "action.php?action=update" method="post">
33. <input type="hidden" name="id" value="<?php echo $news\['id'\]; ?>" />
34. <table width="320" border="1">
35. <tr>
36. <td align="right">标题:</td>
37. <td><input type="text" name="title" value="<?php echo $news\['title'\]; ?>" /></td>
38. </tr>
39. <tr>
40. <td align="right">关键字:</td>
41. <td><input type="text" name="keywords" value="<?php echo $news\['keywords'\]; ?>" /></td>
42. </tr>
43. <tr>
44. <td align="right">作者:</td>
45. <td><input type="text" name="author" value="<?php echo $news\['author'\]; ?>" /></td>
46. </tr>
47. <tr>
48. <td align="right" valign="top">内容:</td>
49. <td><textarea cols="25" rows="5" name="content"><?php echo $news\['content'\]; ?></textarea></td>
50. </tr>
51. <tr>
52. <td colspan="2" align="center">
53. <input type="submit" value="编辑"/>
54. <input type="reset" value="重置"/>
56. </td>
57. </tr>
58. </table>
59. </form>
60. </center>
61. </body>
62. </html>
https://tts.tmooc.cn/ttsPage/NTD/NTDTN202109/WEBBASE/DAY05/EXERCISE/01/index_answer.html