国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP實現簡單的新聞發布系統實例

2024-05-04 23:38:11
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了PHP實現簡單的新聞發布系統,涉及php實現新聞發布系統的sql查詢、插入、更新等完整操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了PHP實現簡單的新聞發布系統。分享給大家供大家參考。具體如下:

本人小白,一直在公司用模板和框架寫PHP,發現有時候連基本的sql語句都忘記了,所以有空想把PHP基礎復習下,鞏固下。分頁和搜索,以及排序,還沒寫,后期繼續更新...(代碼修改:添加搜索和分頁功能)

articlePublish.html:

 

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  
  2. "http://www.w3.org/TR/html4/loose.dtd"
  3. <html lang="en"
  4. <head> 
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf8"
  6. <title>Insert title here</title> 
  7. </head> 
  8. <body bgcolor="#ccc"
  9. <form name="article" method="post" action="articlePublishDo.php" style="margin:5px 500px;"
  10. <h1>發布新聞系統</h1> 
  11. 標題:<input type="text" name="title"/><br/> 
  12. 內容:<textarea cols=30 rows=5 name="content"></textarea><br/><br/> 
  13. <input type="submit" value="發布新聞"/> 
  14. </form> 
  15. </body> 
  16. </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. mysql_close();//關閉數據庫 

articleList.php:

 

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  2. "http://www.w3.org/TR/html4/loose.dtd"
  3. <html> 
  4. <head> 
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf8"
  6. <title>Insert title here</title> 
  7. </head> 
  8. <body> 
  9. <!-- 
  10. 搜索框 
  11. --> 
  12. <form method="get" action="articleList.php" style="margin:10px 400px;"
  13. <input type="text" name="search"/> 
  14. <input type="submit" value="搜索"/> 
  15. </form> 
  16. <br/> 
  17. <table cellspacing="0" cellpadding="0" align="center" bgcolor="#ccc" width=500 > 
  18. <a href="articlePublish.html" style="padding:20px 30px">返回發布文章</a> 
  19. <tr> 
  20. <th>編號</th> 
  21. <th>文章標題</th> 
  22. <th>文章內容</th> 
  23. <th>編輯文章</th> 
  24. </tr> 
  25. <?php 
  26. require_once 'init.php'
  27. /** 
  28. * 搜索 
  29. */ 
  30. $keyword=$_GET['search']; 
  31. /*分頁*/ 
  32. $sql="select count(*) from article where title like '%$keyword%' or content like '%$keyword%'"
  33. $res=mysql_query($sql); 
  34. //$count= (int)mysql_num_rows($result); 
  35. $arr=mysql_fetch_assoc($res); 
  36. while(list($key,$val)=each($arr)){ 
  37. $count = (int)$val;  
  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. //$currentPage = $_GET['page']; 
  45. if($_GET['page'] <=1){ 
  46. $currentPage = 1; 
  47. }elseif ($_GET['page'] >= $page){ 
  48. $currentPage = $page-1; 
  49. }else
  50. $currentPage = $_GET['page']; 
  51. }else 
  52. $currentPage=1; 
  53. $start = ($currentPage-1)*$pageSize; 
  54. $sql="select id,title,content from article where title like '%$keyword%' or content like '%$keyword%' limit $start,$pageSize"
  55. //echo $sql; 
  56. $re=mysql_query($sql);//執行sql語句 
  57. while($arr=mysql_fetch_assoc($re)){ 
  58. ?>  
  59. <tr> 
  60. <td align="center" style="border:1px solid #000"><?php echo $arr['id'];?></td> 
  61. <input type="hidden" name="id" value="<?php echo $arr['id'];?>"/> 
  62. <td align="center" style="border:1px solid #000"><?php echo $arr['title'];?></td> 
  63. <td align="center" style="border:1px solid #000"><?php echo $arr['content'];?></td> 
  64. <td align="center" style="border:1px solid #000"
  65. <a href="articleEdit.php?id=<?php echo $arr['id']?>"><font color="red">修改</font></a> 
  66. <a href="articleDelete.php?id=<?php echo $arr['id']?>"><font color="red">刪除</font></a> 
  67. </td> 
  68. </tr> 
  69. <?php  
  70. mysql_close();//關閉數據庫 
  71. ?> 
  72. </table> 
  73. <div style="margin:20px 400px;"
  74. 共<?php echo $page?>頁 |查到<?php echo $count;?>條記錄 
  75. 當前第<?php echo $_GET['page']?>頁| 
  76. <a href="articleList.php?page=1&search=<?php echo $keyword?>">首頁</a> 
  77. <a href="articleList.php?page=<?php echo ($currentPage-1)?>&search=<?php echo $keyword?>">|上一頁</a> 
  78. <a href="articleList.php?page=<?php echo ($currentPage+1)?>&search=<?php echo $keyword?>">|下一頁</a> 
  79. <a href="articleList.php?page=<?php echo $page?>&search=<?php echo $keyword?>">|末頁</a> 
  80. </div> 
  81. </body> 
  82. </html> 

articleEdit.php:

 

 
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" 
  2. "http://www.w3.org/TR/html4/loose.dtd"
  3. <html lang="en"
  4. <head> 
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf8"
  6. <title>Insert title here</title> 
  7. </head> 
  8. <body bgcolor="#ccc"
  9. <?php  
  10. $id=(int)$_GET['id']; 
  11. require_once 'init.php'
  12. $sql="select id,title,content from article where id = '$id'"
  13. //echo $sql; 
  14. $re=mysql_query($sql);//執行sql語句 
  15. $arr=mysql_fetch_assoc($re); 
  16. //var_dump($arr); 
  17. mysql_close();//關閉數據庫 
  18.  
  19. ?> 
  20. <form name="article" method="post" action="articleUpdate.php" style="margin:5px 500px;"
  21. <h1>文章發布系統</h1> 
  22. <input type="hidden" name="id" value="<?php echo $arr['id']?>"/><br/> 
  23. 標題:<input type="text" name="title" value="<?php echo $arr['title']?>"/><br/> 
  24. 內容:<textarea cols=30 rows=5 name="content"><?php echo $arr['content']?></textarea><br/><br/> 
  25. <input type="submit" value="修改文章"/> 
  26. <a href="articleList.php">返回文章列表</a> 
  27. <a href="articlePublish.html">返回發布文章</a> 
  28. </form> 
  29. </body> 
  30. </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. 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>"

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");//設置交互字符集 

基礎知識總結:

文章發布系統

1.articlePublish.html 發布文章頁面 提交到articlePublishDo.php頁面,執行寫入數據庫

2.articleList.php 文章列表頁面

3.點擊編輯,修改文章 提交到 aiticleEdit.php 表單頁面(回顯)

4.點擊修改文章按鈕 提交到 articleUpdate.php

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 吉安市| 平遥县| 长兴县| 通海县| 荣成市| 梧州市| 东阿县| 多伦县| 晋宁县| 石嘴山市| 名山县| 阳城县| 曲松县| 白沙| 闽侯县| 萍乡市| 宿迁市| 瑞昌市| 大新县| 景东| 房产| 紫金县| 巴彦县| 泸西县| 休宁县| 乌兰县| 肥城市| 楚雄市| 霍城县| 虹口区| 万州区| 敖汉旗| 莆田市| 阳谷县| 边坝县| 禄丰县| 大安市| 朔州市| 阳新县| 中牟县| 塔城市|