index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<html> <head> <title>新闻管理系统</title> <script type="text/javascript"> function dodel(id){ if(confirm("确定要删除吗?")){ window.location="action.php?action=del&id="+id; } } </script> </head> <body> <center> <?php include("menu.php"); //导入导航栏 ?> <h3>浏览新闻</h3> <table width="880" border="1"> <tr> <th>新闻id</th><th>新闻标题</th><th>关键字</th> <th>作者</th><th>发布时间</th><th>新闻内容</th> <th>操作</th> </tr> <?php //1.导入配置文件 require("dbconfig.php"); //2.连接MySQL,选择数据库 $link = @mysql_connect(HOST,USER,PASS) or die("数据库连接失败!"); mysql_select_db(DBNAME,$link); //3. 执行查询,并返回结果集 $sql = "select * from news order by addtime desc"; $result = mysql_query($sql,$link); //4. 解析结果集,并遍历输出 while($row = mysql_fetch_assoc($result)){ echo "<tr>"; echo "<td>{$row['id']}</td>"; echo "<td>{$row['title']}</td>"; echo "<td>{$row['keywords']}</td>"; echo "<td>{$row['author']}</td>"; echo "<td>".date("Y-m-d",$row['addtime'])."</td>"; echo "<td>{$row['content']}</td>"; echo "<td> <a href='javascript:dodel({$row['id']})'>删除</a> <a href='edit.php?id={$row['id']}'>修改</a></td>"; echo "</tr>"; } //5. 释放结果集 mysql_free_result($result); mysql_close($link); ?> </table> </center> </body> </html> |
action.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
<?php //这是一个信息增、删和改操作的处理页 //(1)、 导入配置文件 require("dbconfig.php"); //(2)、连接MySQL、并选择数据库 $link = @mysql_connect(HOST,USER,PASS) or die("数据库连接失败!"); mysql_select_db(DBNAME,$link); //(3)、根据需要action值,来判断所属操作,执行对应的代码 switch($_GET["action"]){ case "add": //执行添加操作 //1. 获取要添加的信息,并补充其他信息 $title = $_POST["title"]; $keywords = $_POST["keywords"]; $author = $_POST["author"]; $content = $_POST["content"]; $addtime = time(); //2. 做信息过滤(省略) //3. 拼装添加SQL语句,并执行添加操作 $sql = "insert into news values(null,'{$title}','{$keywords}','{$author}','{$addtime}','{$content}')"; //echo $sql; mysql_query($sql,$link); //4. 判断是否成功 $id = mysql_insert_id($link);//获取刚刚添加信息的自增id号值 if($id>0){ echo "<h3>新闻信息添加成功!</h3>"; }else{ echo "<h3>新闻信息添加失败!</h3>"; } echo "<a href='javascript:window.history.back();'>返回</a> "; echo "<a href='index.php'>浏览新闻</a>"; break; case "del": //执行删除操作 //1. 获取要删除的id号 $id=$_GET['id']; //2. 拼装删除sql语句,并执行删除操作 $sql = "delete from news where id={$id}"; mysql_query($sql,$link); //3. 自动跳转到浏览新闻界面 header("Location:index.php"); break; case "update": //执行修改操作 //1. 获取要修改的信息 $title = $_POST["title"]; $keywords = $_POST["keywords"]; $author = $_POST["author"]; $content = $_POST["content"]; $id = $_POST['id']; //2. 过滤要修改的信息(省略) //3. 拼装修改sql语句,并执行修改操作 $sql = "update news set title='{$title}',keywords='{$keywords}',author='{$author}',content='{$content}' where id={$id}"; //echo $sql; mysql_query($sql,$link); //4. 跳转回浏览界面 header("Location:index.php"); break; } //(4)、关闭数据连接 mysql_close($link); |
add.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
<html> <head> <title>新闻管理系统</title> </head> <body> <center> <?php include("menu.php"); //导入导航栏 ?> <h3>发布新闻</h3> <form action="action.php?action=add" method="post"> <table width="320" border="0"> <tr> <td align="right">标题:</td> <td><input type="text" name="title"/></td> </tr> <tr> <td align="right">关键字:</td> <td><input type="text" name="keywords"/></td> </tr> <tr> <td align="right">作者:</td> <td><input type="text" name="author"/></td> </tr> <tr> <td align="right" valign="top">内容:</td> <td><textarea cols="25" rows="5" name="content"></textarea></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="添加"/> <input type="reset" value="重置"/> </td> </tr> </table> </form> </center> </body> </html> |
list1.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
<html> <head> <title>新闻管理系统</title> <script type="text/javascript"> function dodel(id){ if(confirm("确定要删除吗?")){ window.location="action.php?action=del&id="+id; } } </script> </head> <body> <center> <?php include("menu.php"); //导入导航栏 ?> <h3>搜索新闻</h3> <!-----搜索表单---------> <form action="list1.php" method="get"> 标题:<input type="text" name="title" size="8" value="<?php echo !empty($_GET['title'])?$_GET['title']:''; ?>" /> 关键字:<input type="text" name="keywords" size="8" value="<?php echo !empty($_GET['keywords'])?$_GET['keywords']:''; ?>" /> 作者:<input type="text" name="author" size="8" value="<?php echo !empty($_GET['author'])?$_GET['author']:''; ?>"/> <input type="submit" value="搜索"/> <input type="button" value="全部信息" onclick="window.location='list1.php'"/> </form> <!--------------> <table width="880" border="1"> <tr> <th>新闻id</th><th>新闻标题</th><th>关键字</th> <th>作者</th><th>发布时间</th><th>新闻内容</th> <th>操作</th> </tr> <?php //============================= //封装搜索信息 $wherelist = array();//定义一个封装搜索条件的数组变量 //判断新闻标题是否有值,若有则封装此搜索条件 if(!empty($_GET["title"])){ $wherelist[]="title like '%{$_GET['title']}%'"; } //判断关键字是否有值,若有则封装此搜索条件 if(!empty($_GET["keywords"])){ $wherelist[]="keywords like '%{$_GET['keywords']}%'"; } //判断作者是否有值,若有则封装此搜索条件 if(!empty($_GET["author"])){ $wherelist[]="author like '%{$_GET['author']}%'"; } //组装搜索条件 if(count($wherelist)>0){ $where = " where ".implode(" and ",$wherelist); } //echo $where; //============================= //1.导入配置文件 require("dbconfig.php"); //2.连接MySQL,选择数据库 $link = @mysql_connect(HOST,USER,PASS) or die("数据库连接失败!"); mysql_select_db(DBNAME,$link); //3. 执行查询,并返回结果集 @$sql = "select * from news {$where} order by addtime desc"; $result = mysql_query($sql,$link); //4. 解析结果集,并遍历输出 while($row = mysql_fetch_assoc($result)){ echo "<tr>"; echo "<td>{$row['id']}</td>"; echo "<td>{$row['title']}</td>"; echo "<td>{$row['keywords']}</td>"; echo "<td>{$row['author']}</td>"; echo "<td>".date("Y-m-d",$row['addtime'])."</td>"; echo "<td>{$row['content']}</td>"; echo "<td> <a href='javascript:dodel({$row['id']})'>删除</a> <a href='edit.php?id={$row['id']}'>修改</a></td>"; echo "</tr>"; } //5. 释放结果集 mysql_free_result($result); mysql_close($link); ?> </table> </center> </body> </html> |
list2.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
<html> <head> <title>新闻管理系统</title> <script type="text/javascript"> function dodel(id){ if(confirm("确定要删除吗?")){ window.location="action.php?action=del&id="+id; } } </script> </head> <body> <center> <?php include("menu.php"); //导入导航栏 ?> <h3>分页浏览新闻</h3> <table width="880" border="1"> <tr> <th>新闻id</th><th>新闻标题</th><th>关键字</th> <th>作者</th><th>发布时间</th><th>新闻内容</th> <th>操作</th> </tr> <?php //1.导入配置文件 require("dbconfig.php"); //2.连接MySQL,选择数据库 $link = @mysql_connect(HOST,USER,PASS) or die("数据库连接失败!"); mysql_select_db(DBNAME,$link); //2.1 插入分页处理代码 //====================================== //1. 定义一些分页变量 $page=isset($_GET["page"])?$_GET['page']:1; //当前页号 $pageSize=3; //页大小 $maxRows; //最大数据条 $maxPages; //最大页数 //2. 获取最大数据条数 $sql = "select count(*) from news"; $res = mysql_query($sql,$link); $maxRows = mysql_result($res,0,0); //定位从结果集中获取总数据条数这个值。 //3. 计算出共计最大页数 $maxPages = ceil($maxRows/$pageSize); //采用进一求整法算出最大页数 //4. 效验当前页数 if($page>$maxPages){ $page=$maxPages; } if($page<1){ $page=1; } //5. 拼装分页sql语句片段 $limit = " limit ".(($page-1)*$pageSize).",{$pageSize}"; //起始位置是当前页减一乘以页大小 //====================================== //3. 执行查询,并返回结果集 $sql = "select * from news order by addtime desc {$limit}"; $result = mysql_query($sql,$link); //4. 解析结果集,并遍历输出 while($row = mysql_fetch_assoc($result)){ echo "<tr>"; echo "<td>{$row['id']}</td>"; echo "<td>{$row['title']}</td>"; echo "<td>{$row['keywords']}</td>"; echo "<td>{$row['author']}</td>"; echo "<td>".date("Y-m-d",$row['addtime'])."</td>"; echo "<td>{$row['content']}</td>"; echo "<td> <a href='javascript:dodel({$row['id']})'>删除</a> <a href='edit.php?id={$row['id']}'>修改</a></td>"; echo "</tr>"; } //5. 释放结果集 mysql_free_result($result); mysql_close($link); ?> </table> <?php //输出分页信息,显示上一页和下一页的连接 echo "<br/><br/>"; echo "当前{$page}/{$maxPages}页 共计{$maxRows}条"; echo " <a href='list2.php?page=1'>首页</a> "; echo " <a href='list2.php?page=".($page-1)."'>上一页</a> "; echo " <a href='list2.php?page=".($page+1)."'>下一页</a> "; echo " <a href='list2.php?page={$maxPages}'>末页</a> "; ?> </center> </body> </html> |
list3.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
<html> <head> <title>新闻管理系统</title> <script type="text/javascript"> function dodel(id){ if(confirm("确定要删除吗?")){ window.location="action.php?action=del&id="+id; } } </script> </head> <body> <center> <?php include("menu.php"); //导入导航栏 ?> <h3>分页浏览新闻</h3> <!-----搜索表单---------> <form action="list3.php" method="get"> 标题:<input type="text" name="title" size="8" value="<?php echo !empty($_GET['title'])?$_GET['title']:""; ?>" /> 关键字:<input type="text" name="keywords" size="8" value="<?php echo !empty($_GET['keywords'])?$_GET['keywords']:""; ?>" /> 作者:<input type="text" name="author" size="8" value="<?php echo !empty($_GET['author'])?$_GET['author']:""; ?>"/> <input type="submit" value="搜索"/> <input type="button" value="全部信息" onclick="window.location='list3.php'"/> </form> <!--------------> <table width="880" border="1"> <tr> <th>新闻id</th><th>新闻标题</th><th>关键字</th> <th>作者</th><th>发布时间</th><th>新闻内容</th> <th>操作</th> </tr> <?php //============================= //封装搜索信息 $wherelist = array();//定义一个封装搜索条件的数组变量 $urllist = array(); //定义了一个封装搜索条件的url数组,语句放置到url后面做url参数使用 //判断新闻标题是否有值,若有则封装此搜索条件 if(!empty($_GET["title"])){ $wherelist[]="title like '%{$_GET['title']}%'"; $urllist[]="title={$_GET['title']}"; } //判断关键字是否有值,若有则封装此搜索条件 if(!empty($_GET["keywords"])){ $wherelist[]="keywords like '%{$_GET['keywords']}%'"; $urllist[]="keywords={$_GET['keywords']}"; } //判断作者是否有值,若有则封装此搜索条件 if(!empty($_GET["author"])){ $wherelist[]="author like '%{$_GET['author']}%'"; $urllist[]="author={$_GET['author']}"; } //组装搜索条件 if(count($wherelist)>0){ $where = " where ".implode(" and ",$wherelist); $url = "&".implode("&",$urllist); } //echo $where; //echo $url; //============================= //1.导入配置文件 require("dbconfig.php"); //2.连接MySQL,选择数据库 $link = @mysql_connect(HOST,USER,PASS) or die("数据库连接失败!"); mysql_select_db(DBNAME,$link); //2.1 插入分页处理代码 //====================================== //1. 定义一些分页变量 $page=isset($_GET["page"])?$_GET['page']:1; //当前页号 $pageSize=3; //页大小 $maxRows; //最大数据条 $maxPages; //最大页数 //2. 获取最大数据条数 @$sql = "select count(*) from news {$where}"; $res = mysql_query($sql,$link); $maxRows = mysql_result($res,0,0); //定位从结果集中获取总数据条数这个值。 //3. 计算出共计最大页数 $maxPages = ceil($maxRows/$pageSize); //采用进一求整法算出最大页数 //4. 效验当前页数 if($page>$maxPages){ $page=$maxPages; } if($page<1){ $page=1; } //5. 拼装分页sql语句片段 $limit = " limit ".(($page-1)*$pageSize).",{$pageSize}"; //起始位置是当前页减一乘以页大小 //====================================== //3. 执行查询,并返回结果集 @$sql = "select * from news {$where} order by addtime asc {$limit}"; @$result = mysql_query($sql,$link); //4. 解析结果集,并遍历输出 while($row = mysql_fetch_assoc($result)){ echo "<tr>"; echo "<td>{$row['id']}</td>"; echo "<td>{$row['title']}</td>"; echo "<td>{$row['keywords']}</td>"; echo "<td>{$row['author']}</td>"; echo "<td>".date("Y-m-d",$row['addtime'])."</td>"; echo "<td>{$row['content']}</td>"; echo "<td> <a href='javascript:dodel({$row['id']})'>删除</a> <a href='edit.php?id={$row['id']}'>修改</a></td>"; echo "</tr>"; } //5. 释放结果集 mysql_free_result($result); mysql_close($link); ?> </table> <?php $url = !empty($url)?$url:""; //输出分页信息,显示上一页和下一页的连接 echo "<br/><br/>"; echo "当前{$page}/{$maxPages}页 共计{$maxRows}条"; echo " <a href='list3.php?page=1{$url}'>首页</a> "; echo " <a href='list3.php?page=".($page-1)."{$url}'>上一页</a> "; echo " <a href='list3.php?page=".($page+1)."{$url}'>下一页</a> "; echo " <a href='list3.php?page={$maxPages}{$url}'>末页</a> "; ?> </center> </body> </html> |
menu.php
1 2 3 4 5 6 7 |
<h2>新闻管理系统</h2> <a href="index.php">浏览新闻</a> | <a href="add.php">发布新闻</a> | <a href="list1.php">搜索新闻</a> | <a href="list2.php">分页浏览新闻</a> | <a href="list3.php">搜索加分页浏览新闻</a> <hr width="90%"/> |
edit.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
<html> <head> <title>新闻管理系统</title> </head> <body> <center> <?php include("menu.php"); //导入导航栏 //1. 导入配置文件 require("dbconfig.php"); //2. 连接MySQL、选择数据库 $link = @mysql_connect(HOST,USER,PASS) or die("数据库连接失败!"); mysql_select_db(DBNAME,$link); //3. 获取要修改信息的id号,并拼装查看sql语句,执行查询,获取要修改的信息 $sql = "select * from news where id={$_GET['id']}"; $result = mysql_query($sql,$link); //4. 判断是否获取到了要修改的信息 if($result && mysql_num_rows($result)>0){ $news = mysql_fetch_assoc($result); }else{ die("没有找到要修改的信息!"); } ?> <h3>编辑新闻</h3> <form action="action.php?action=update" method="post"> <input type="hidden" name="id" value="<?php echo $news['id']; ?>"/> <table width="320" border="0"> <tr> <td align="right">标题:</td> <td><input type="text" name="title" value="<?php echo $news['title']; ?>"/></td> </tr> <tr> <td align="right">关键字:</td> <td><input type="text" name="keywords" value="<?php echo $news['keywords']; ?>"/></td> </tr> <tr> <td align="right">作者:</td> <td><input type="text" name="author" value="<?php echo $news['author']; ?>"/></td> </tr> <tr> <td align="right" valign="top">内容:</td> <td><textarea cols="25" rows="5" name="content"><?php echo $news['content']; ?></textarea></td> </tr> <tr> <td colspan="2" align="center"> <input type="submit" value="编辑"/> <input type="reset" value="重置"/> </td> </tr> </table> </form> </center> </body> </html> |
dbconfig.php
1 2 3 4 5 6 7 8 |
<?php //公共信息配置文件 //数据库信息配 define("HOST","localhost"); //主机名 define("USER","root"); //用户名 define("PASS","root"); //密码 define("DBNAME","demodb"); //数据库名 |
数据库sql
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for goods -- ---------------------------- DROP TABLE IF EXISTS `goods`; CREATE TABLE `goods` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(64) NOT NULL, `typeid` int(10) unsigned NOT NULL, `price` double(6,2) unsigned NOT NULL, `total` int(10) unsigned NOT NULL, `pic` varchar(32) NOT NULL, `note` text, `addtime` int(10) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of goods -- ---------------------------- INSERT INTO `goods` VALUES ('7', '范德萨', '1', '88.00', '1000', '201609260942297599.png', '可以的', '1474854149'); INSERT INTO `goods` VALUES ('5', 'ddd', '1', '11.00', '11', '201609260333188393.jpg', '范德萨分', '1474831998'); -- ---------------------------- -- Table structure for news -- ---------------------------- DROP TABLE IF EXISTS `news`; CREATE TABLE `news` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `title` varchar(64) NOT NULL, `keywords` varchar(64) NOT NULL, `author` varchar(16) NOT NULL, `addtime` int(10) unsigned NOT NULL, `content` text NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=8 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of news -- ---------------------------- INSERT INTO `news` VALUES ('1', '做一个善良的人', '做一个善良的人', '做一个善良的人', '1474873680', '做一个善良的人做一个善良的人做一个善良的人做一个善良的人做一个善良的人做一个善良的人'); INSERT INTO `news` VALUES ('2', '素材火,你好。', '做一个善良的人', '做一个善良的人', '1474873694', '做一个善良的人'); INSERT INTO `news` VALUES ('3', '素材火,你好。', '做一个善良的人', '做一个善良的人', '1474873697', '做一个善良的人'); INSERT INTO `news` VALUES ('4', '素材火,你好。', '做一个善良的人', '做一个善良的人', '1474873699', '做一个善良的人'); INSERT INTO `news` VALUES ('5', '素材火,你好。', '做一个善良的人', '做一个善良的人', '1474873701', '做一个善良的人'); INSERT INTO `news` VALUES ('6', '素材火,你好。', '做一个善良的人', '做一个善良的人', '1474873893', '做一个善良的人'); INSERT INTO `news` VALUES ('7', '素材火,你好。', '做一个善良的人', '做一个善良的人', '1474873895', '做一个善良的人'); -- ---------------------------- -- Table structure for type -- ---------------------------- DROP TABLE IF EXISTS `type`; CREATE TABLE `type` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(64) NOT NULL, `pid` int(10) unsigned NOT NULL, `path` varchar(128) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8; -- ---------------------------- -- Records of type -- ---------------------------- INSERT INTO `type` VALUES ('1', '新闻', '0', '0,'); INSERT INTO `type` VALUES ('2', '体育', '0', '0,'); INSERT INTO `type` VALUES ('3', '国内新闻', '1', '0,1,'); INSERT INTO `type` VALUES ('4', '国外新闻', '1', '0,1,'); INSERT INTO `type` VALUES ('5', '十九大召开', '3', '0,1,3,'); INSERT INTO `type` VALUES ('6', '国外体育', '2', '0,2,'); INSERT INTO `type` VALUES ('7', '国内体育', '2', '0,2,'); INSERT INTO `type` VALUES ('8', 'NBA', '6', '0,2,6,'); INSERT INTO `type` VALUES ('9', '中国足球', '7', '0,2,7,'); |