当前位置:Gxlcms > PHP教程 > php实现新闻发布系统

php实现新闻发布系统

时间:2021-07-01 10:21:17 帮助过:5人阅读

本篇文章主要介绍php实现新闻发布系统,感兴趣的朋友参考下,希望对大家有所帮助。

articlePublish.html:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf8">
  5. <title>Insert title here</title>
  6. </head>
  7. <body bgcolor="#ccc">
  8. <form name="article" method="post" action="articlePublishDo.php" style="margin:5px 500px;">
  9. <h1>发布新闻系统</h1>
  10. 标题:<input type="text" name="title"/><br/>
  11. 内容:<textarea cols=30 rows=5 name="content"></textarea><br/><br/>
  12. <input type="submit" value="发布新闻"/>
  13. </form>
  14. </body>
  15. </html>

articlePublishDo.php:

  1. <?php
  2. header("content-type:text/html;charset=utf8");
  3. date_default_timezone_set('Asia/Shanghai');
  4. $title=trim($_POST['title']);
  5. $content=trim($_POST['content']);
  6. $time=date("y-m-d H:i:s");
  7. require_once 'init.php';
  8. $sql="insert into article(title,content,create_time) values('$title','$content','$time')";
  9. //echo $sql;
  10. $re=mysql_query($sql);//执行sql语句
  11. if($re){
  12. echo "发布成功";
  13. echo '<a href="articleList.php">返回文章列表</a>';
  14. }else{
  15. echo "发布失败";
  16. echo '<a href="articleList.php">返回文章列表</a>';
  17. }
  18. mysql_close();//关闭数据库

articleList.php:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf8">
  5. <title>Insert title here</title>
  6. </head>
  7. <body>
  8. <!--
  9. 搜索框
  10. -->
  11. <form method="get" action="articleList.php" style="margin:10px 400px;">
  12. <input type="text" name="search"/>
  13. <input type="submit" value="搜索"/>
  14. </form>
  15. <br/>
  16. <table cellspacing="0" cellpadding="0" align="center" bgcolor="#ccc" width=500 >
  17. <a href="articlePublish.html" style="padding:20px 30px">返回发布文章</a>
  18. <tr>
  19. <th>编号</th>
  20. <th>文章标题</th>
  21. <th>文章内容</th>
  22. <th>编辑文章</th>
  23. </tr>
  24. <?php
  25. require_once 'init.php';
  26. /**
  27. * 搜索
  28. */
  29. $keyword=$_GET['search'];
  30. /*分页*/
  31. $sql="select count(*) from article where title like '%$keyword%' or content like '%$keyword%'";
  32. $res=mysql_query($sql);
  33. //$count= (int)mysql_num_rows($result);
  34. $arr=mysql_fetch_assoc($res);
  35. while(list($key,$val)=each($arr)){
  36. $count = (int)$val;
  37. }
  38. //echo $count;
  39. $pageSize=4;
  40. $page=floor($count/$pageSize)+1;//总页数$page
  41. echo $page;
  42. //echo $page;
  43. if(isset($_GET['page']))
  44. {
  45. //$currentPage = $_GET['page'];
  46. if($_GET['page'] <=1){
  47. $currentPage = 1;
  48. }elseif ($_GET['page'] >= $page){
  49. $currentPage = $page-1;
  50. }else{
  51. $currentPage = $_GET['page'];
  52. }
  53. }else
  54. {
  55. $currentPage=1;
  56. }
  57. $start = ($currentPage-1)*$pageSize;
  58. $sql="select id,title,content from article where title like '%$keyword%' or content like '%$keyword%' limit $start,$pageSize";
  59. //echo $sql;
  60. $re=mysql_query($sql);//执行sql语句
  61. while($arr=mysql_fetch_assoc($re)){
  62. ?>
  63. <tr>
  64. <td align="center" style="border:1px solid #000"><?php echo $arr['id'];?></td>
  65. <input type="hidden" name="id" value="<?php echo $arr['id'];?>"/>
  66. <td align="center" style="border:1px solid #000"><?php echo $arr['title'];?></td>
  67. <td align="center" style="border:1px solid #000"><?php echo $arr['content'];?></td>
  68. <td align="center" style="border:1px solid #000">
  69. <a href="articleEdit.php?id=<?php echo $arr['id']?>"><font color="red">修改</font></a>
  70. <a href="articleDelete.php?id=<?php echo $arr['id']?>"><font color="red">删除</font></a>
  71. </td>
  72. </tr>
  73. <?php
  74. }
  75. mysql_close();//关闭数据库
  76. ?>
  77. </table>
  78. <p style="margin:20px 400px;">
  79. 共<?php echo $page?>页 |查到<?php echo $count;?>条记录
  80. 当前第<?php echo $_GET['page']?>页|
  81. <a href="articleList.php?page=1&search=<?php echo $keyword?>">首页</a>
  82. <a href="articleList.php?page=<?php echo ($currentPage-1)?>&search=<?php echo $keyword?>">|上一页</a>
  83. <a href="articleList.php?page=<?php echo ($currentPage+1)?>&search=<?php echo $keyword?>">|下一页</a>
  84. <a href="articleList.php?page=<?php echo $page?>&search=<?php echo $keyword?>">|末页</a>
  85. </p>
  86. </body>
  87. </html>

articleEdit.php:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf8">
  5. <title>Insert title here</title>
  6. </head>
  7. <body bgcolor="#ccc">
  8. <?php
  9. $id=(int)$_GET['id'];
  10. require_once 'init.php';
  11. $sql="select id,title,content from article where id = '$id'";
  12. //echo $sql;
  13. $re=mysql_query($sql);//执行sql语句
  14. $arr=mysql_fetch_assoc($re);
  15. //var_dump($arr);
  16. mysql_close();//关闭数据库
  17. ?>
  18. <form name="article" method="post" action="articleUpdate.php" style="margin:5px 500px;">
  19. <h1>文章发布系统</h1>
  20. <input type="hidden" name="id" value="<?php echo $arr['id']?>"/><br/>
  21. 标题:<input type="text" name="title" value="<?php echo $arr['title']?>"/><br/>
  22. 内容:<textarea cols=30 rows=5 name="content"><?php echo $arr['content']?></textarea><br/><br/>
  23. <input type="submit" value="修改文章"/>
  24. <a href="articleList.php">返回文章列表</a>
  25. <a href="articlePublish.html">返回发布文章</a>
  26. </form>
  27. </body>
  28. </html>

articleUpdate.php:

  1. <?php
  2. header("content-type:text/html;charset=utf8");
  3. $arr=$_POST;
  4. $id=(int)$arr['id'];
  5. require_once 'init.php';
  6. $sql="update article set title = '$arr[title]',content = '$arr[content]' where id = '$id'";
  7. //echo $sql;
  8. $re=mysql_query($sql);//执行sql语句
  9. //echo $re;
  10. if($re){
  11. echo "修改成功";
  12. echo "<a href='articleList.php'>返回文章列表</a>";
  13. }else{
  14. echo "修改失败";
  15. echo "<a href='articleList.php'>返回文章列表</a>";
  16. }
  17. mysql_close();//关闭数据库

articleDelete.php:

  1. <?php
  2. header("content-type:text/html;charset=utf8");
  3. require_once 'init.php';
  4. $id=(int)$_GET['id'];
  5. $sql="delete from article where id = '$id'";
  6. //echo $sql;
  7. $re=mysql_query($sql);
  8. if($re){
  9. echo "删除成功";
  10. echo "<a href='articleList.php'>返回文章列表</a>";
  11. }else{
  12. echo "删除失败";
  13. echo "<a href='articleList.php'>返回文章列表</a>";
  14. }

init.php:

  1. <?php
  2. //连接数据库
  3. //五步走
  4. //往数据库添加文章
  5. $conn=mysql_connect("localhost","root","");//链接数据库
  6. //echo $conn;
  7. $re=mysql_select_db("article");//选择数据库
  8. mysql_query("set names utf8");//设置交互字符集

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

php可逆加密的方法及原理

php针对字符串的遍历与截取操作技巧

php实现发送和接收短信的功能

以上就是php实现新闻发布系统的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行