1 实现一个小型的新闻管理项目,包括对新闻信息的增删改查。页面功能如图 - 1、图 - 2 所示。

EXERCISE - 图1

图 - 1

EXERCISE - 图2

图 - 2

参考答案

在 phpStudy 的安装目录下,找到 www 子目录,.php 文件位置要在该目录下。

1)创建 dbconfig.php 文件,功能是数据库信息的设置,内容如下:

  1. 1. <?php
  2. 4. define("HOST","localhost");
  3. 5. define("USER","root");
  4. 6. define("PASS","root");
  5. 7. define("DBNAME","newsdb");
  6. 8. ?>

2)创建 menu.php 文件,功能是导航菜单的设置,内容如下:

  1. 1. <h2>新闻管理系统</h2>
  2. 2. <a href="index.php">浏览新闻</a>
  3. 3. <a href="add.php">发布新闻</a>
  4. 4. <hr width="90%"/>

3)创建 index.php 文件,功能是新闻信息的浏览,内容如下:

  1. 1. <html>
  2. 2. <head>
  3. 3. <meta charset="UTF-8">
  4. 4. <title>新闻管理系统</title>
  5. 5. <script type="text/javascript">
  6. 6. function dodel(id)
  7. 7. {
  8. 8. if(confirm("确定要删除吗"))
  9. 9. {
  10. 10. window.location="action.php?action=del&id="+id;
  11. 11. }
  12. 12. }
  13. 13. </script>
  14. 14. </head>
  15. 15. <body>
  16. 16. <center>
  17. 17. <?php include("menu.php");
  18. 19. <h3>浏览新闻</h3>
  19. 20. <table width="800" border="1">
  20. 21. <tr>
  21. 22. <th>新闻id</th>
  22. 23. <th>新闻标题</th>
  23. 24. <th>关键字</th>
  24. 25. <th>作者</th>
  25. 26. <th>发布时间</th>
  26. 27. <th>新闻内容</th>
  27. 28. <th>操作</th>
  28. 29. </tr>
  29. 30. <?php
  30. 31. date\_default\_timezone_set("PRC");
  31. 33. require("dbconfig.php");
  32. 35. $link = @mysqli_connect(HOST,USER,PASS) or die("数据库连接失败!");
  33. 36. mysqli\_select\_db($link,DBNAME);
  34. 38. $sql = "select * from news order by addtime desc";
  35. 39. $result = mysqli_query($link,$sql);
  36. 42. while($row = mysqli\_fetch\_assoc($result))
  37. 43. {
  38. 44. echo "<tr>";
  39. 45. echo "<td>{$row\['id'\]}</td>";
  40. 46. echo "<td>{$row\['title'\]}</td>";
  41. 47. echo "<td>{$row\['keywords'\]}</td>";
  42. 48. echo "<td>{$row\['author'\]}</td>";
  43. 49. echo "<td>".date("Y-m-d",$row\['addtime'\])."</td>";
  44. 50. echo "<td>{$row\['content'\]}</td>";
  45. 51. echo "<td>
  46. 52. <a href='javascript:dodel({$row\['id'\]})'>删除</a>
  47. 53. <a href='edit.php?id={$row\['id'\]}'>修改</a></td>";
  48. 54. echo "</tr>";
  49. 55. }
  50. 58. mysqli\_free\_result($result);
  51. 59. mysqli_close($link);
  52. 60. ?>
  53. 61. </table>
  54. 62. </center>
  55. 63. </body>
  56. 64. </html>

4)创建 action.php 文件,功能是数据的增删改查的实现,内容如下:

  1. 1. <?php
  2. 3. header("Content-type: text/html; charset=utf-8");
  3. 7. require("dbconfig.php");
  4. 9. $link=@mysqli_connect(HOST,USER,PASS) or die("数据库连接失败!");
  5. 10. mysqli\_select\_db($link,DBNAME);
  6. 13. switch($_GET\["action"\])
  7. 14. {
  8. 15. case "add":
  9. 17. $title = $_POST\["title"\];
  10. 18. $keywords = $_POST\["keywords"\];
  11. 19. $author = $_POST\["author"\];
  12. 20. $content = $_POST\["content"\];
  13. 21. $addtime = time();
  14. 24. $sql = "insert into news values(null,'{$title}','{$keywords}','{$author}','{$addtime}','{$content}')";
  15. 25. mysqli_query($link,$sql);
  16. 27. $id=mysqli\_insert\_id($link);
  17. 28. if($id>0)
  18. 29. {
  19. 30. echo "<h3>新闻信息添加成功!</h3>";
  20. 31. }else
  21. 32. {
  22. 33. echo "<h3>新闻信息添加失败!</h3>";
  23. 34. }
  24. 35. echo "<a href='javascript:window.history.back();'>返回</a> ";
  25. 36. echo "<a href='index.php'>浏览新闻</a>";
  26. 37. break;
  27. 38. case "del":
  28. 40. $id=$_GET\['id'\];
  29. 42. $sql = "delete from news where id={$id}";
  30. 43. mysqli_query($link,$sql);
  31. 46. header("Location:index.php");
  32. 47. break;
  33. 48. case "update":
  34. 50. $title = $_POST\['title'\];
  35. 51. $keywords = $_POST\['keywords'\];
  36. 52. $author = $_POST\['author'\];
  37. 53. $content = $_POST\['content'\];
  38. 54. $id = $_POST\['id'\];
  39. 58. $sql = "update news set title='{$title}',keywords='{$keywords}',author='{$author}',content='{$content}' where id = {$id} ";
  40. 60. mysqli_query($link,$sql);
  41. 62. header("Location:index.php");
  42. 63. break;
  43. 64. }
  44. 66. mysqli_close($link);

5)创建 add.php 文件,功能是数据信息的设置,内容如下:

  1. 1. <html>
  2. 2. <head>
  3. 3. <meta charset="UTF-8">
  4. 4. <title>新闻管理系统</title>
  5. 5. </head>
  6. 6. <body>
  7. 7. <center>
  8. 8. <?php include("menu.php");
  9. 10. <h3>发布新闻</h3>
  10. 11. <form action = "action.php?action=add" method="post">
  11. 12. <table width="320" border="1">
  12. 13. <tr>
  13. 14. <td align="right">标题:</td>
  14. 15. <td><input type="text" name="title"/></td>
  15. 16. </tr>
  16. 17. <tr>
  17. 18. <td align="right">关键字:</td>
  18. 19. <td><input type="text" name="keywords"/></td>
  19. 20. </tr>
  20. 21. <tr>
  21. 22. <td align="right">作者:</td>
  22. 23. <td><input type="text" name="author"/></td>
  23. 24. </tr>
  24. 25. <tr>
  25. 26. <td align="right" valign="top">内容:</td>
  26. 27. <td><textarea cols="25" rows="5" name="content"></textarea></td>
  27. 28. </tr>
  28. 29. <tr>
  29. 30. <td colspan="2" align="center">
  30. 31. <input type="submit" value="添加"/>
  31. 32. <input type="reset" value="重置"/>
  32. 34. </td>
  33. 35. </tr>
  34. 36. </table>
  35. 37. </form>
  36. 38. </center>
  37. 39. </body>
  38. 40. </html>

6)创建 edit.php 文件,功能是数据信息的设置,内容如下:

  1. 1. <html>
  2. 2. <head>
  3. 3. <meta charset="UTF-8">
  4. 4. <title>新闻管理系统</title>
  5. 5. </head>
  6. 6. <body>
  7. 7. <center>
  8. 8. <?php
  9. 9. include("menu.php");
  10. 12. require("dbconfig.php");
  11. 15. $link = @mysqli_connect(HOST,USER,PASS) or die("数据库连接失败!");
  12. 16. mysqli\_select\_db($link,DBNAME);
  13. 18. $sql = "select *from news where id={$_GET\['id'\]}";
  14. 19. $result = mysqli_query($link,$sql);
  15. 21. if($result &&mysqli\_num\_rows($result)>0)
  16. 22. {
  17. 23. $news = mysqli\_fetch\_assoc($result);
  18. 24. }else
  19. 25. {
  20. 26. die("没有找到要修改的信息!");
  21. 27. }
  22. 29. ?>
  23. 31. <h3>编辑新闻</h3>
  24. 32. <form action = "action.php?action=update" method="post">
  25. 33. <input type="hidden" name="id" value="<?php echo $news\['id'\]; ?>" />
  26. 34. <table width="320" border="1">
  27. 35. <tr>
  28. 36. <td align="right">标题:</td>
  29. 37. <td><input type="text" name="title" value="<?php echo $news\['title'\]; ?>" /></td>
  30. 38. </tr>
  31. 39. <tr>
  32. 40. <td align="right">关键字:</td>
  33. 41. <td><input type="text" name="keywords" value="<?php echo $news\['keywords'\]; ?>" /></td>
  34. 42. </tr>
  35. 43. <tr>
  36. 44. <td align="right">作者:</td>
  37. 45. <td><input type="text" name="author" value="<?php echo $news\['author'\]; ?>" /></td>
  38. 46. </tr>
  39. 47. <tr>
  40. 48. <td align="right" valign="top">内容:</td>
  41. 49. <td><textarea cols="25" rows="5" name="content"><?php echo $news\['content'\]; ?></textarea></td>
  42. 50. </tr>
  43. 51. <tr>
  44. 52. <td colspan="2" align="center">
  45. 53. <input type="submit" value="编辑"/>
  46. 54. <input type="reset" value="重置"/>
  47. 56. </td>
  48. 57. </tr>
  49. 58. </table>
  50. 59. </form>
  51. 60. </center>
  52. 61. </body>
  53. 62. </html>

https://tts.tmooc.cn/ttsPage/NTD/NTDTN202109/WEBBASE/DAY05/EXERCISE/01/index_answer.html