PHP基础示例:用PHP+Mysql编写简易新闻管理系统
实现目标:使用php和mysql操作函数实现一个新闻信息的发布、浏览、修改和删除操作
实现步骤:
一、创建数据库和表
1.创建数据库和表:newsdb
2.创建表格:news
字段:新闻id,标题,关键字,作者,发布时间,新闻内容
二、创建php文件编写代码(以下为要创建的php文件及其用途)
dbconfig.php 公共配置文件,数据库连接配置信息
menu.php 网站公共导航栏
index.php 浏览新闻的文件(此为首页)
add.php 发布新闻表单页
edit.php 编辑新闻的表单页
action.php 执行新闻信息添加、修改、删除等操作的动作(后台)
**********************************************************************
以下为数据库创建语句:
1 create database newsdb;//创建数据库语句 2 create table news( 3 id int unsigned not null auto_increment primary key, 4 title varchar(64) not null, 5 keywords varchar(64) not null, 6 author varchar(16) not null, 7 addtime int unsigned not null, 8 content text not null 9 );//创建表语句
数据库创建语句
***********************************************************************
以下为dbconfig.php文件代码
1 <?php 2 //公共信息配置 3 //数据库配置信息 4 define("HOST","localhost"); //主机名 5 define("USER","root"); //账号 6 define("PASS","root"); //密码 7 define("DBNAME","newsdb"); //数据库名 8 ?>
以下为menu.php文件代码(一开始浏览的页面,添加新闻后以index页面为主)
1 <h2>新闻管理系统</h2> 2 <a href="index.php">浏览新闻</a> 3 <a href="add.php">发布新闻</a> 4 <hr width="90%"/>
以下为add.php文件代码(增加具体代码)
1 <html> 2 <head> 3 <title>新闻管理系统</title> 4 </head> 5 <body> 6 <center> 7 <?php include("menu.php");//导入导航栏 ?> 8 9 <h3>发布新闻</h3> 10 <form action = "action.php?action=add" method="post"> 11 <table width="320" border="1"> 12 <tr> 13 <td align="right">标题:</td> 14 <td><input type="text" name="title"/></td> 15 </tr> 16 <tr> 17 <td align="right">关键字:</td> 18 <td><input type="text" name="keywords"/></td> 19 </tr> 20 <tr> 21 <td align="right">作者:</td> 22 <td><input type="text" name="author"/></td> 23 </tr> 24 <tr> 25 <td align="right" valign="top">内容:</td> 26 <td><textarea cols="25" rows="5" name="content"></textarea></td> 27 </tr> 28 <tr> 29 <td colspan="2" align="center"> 30 <input type="submit" value="添加"/> 31 <input type="reset" value="重置"/> 32 33 </td> 34 </tr> 35 </table> 36 </form> 37 </center> 38 </body> 39 </html>
add.php文件代码
以下为action.php文件代码(增删改实现代码)
1 <?php 2 //这是一个信息增、删和改操作的处理页面 3 4 //1.导入配置文件 5 require("dbconfig.php"); 6 //2.连接MYSQL,并选择数据库 7 $link=@mysql_connect(HOST,USER,PASS) or die("数据库连接失败!"); 8 mysql_select_db(DBNAME,$link); 9 10 //3.根据需要action值,来判断所属操作,执行对应的代码 11 switch($_GET["action"]) 12 { 13 case "add": //执行添加操作 14 //1.获取要添加的信息,并补充其他信息 15 $title = $_POST["title"]; 16 $keywords = $_POST["keywords"]; 17 $author = $_POST["author"]; 18 $content = $_POST["content"]; 19 $addtime = time(); 20 //2.座信息过滤(省略) 21 //3.拼装添加SQL语句,并执行添加操作 22 $sql = "insert into news values(null,'{$title}','{$keywords}','{$author}','{$addtime}','{$content}')"; 23 mysql_query($sql,$link); 24 //4.判断是否成功 25 $id=mysql_insert_id($link);//获取刚刚添加信息的自增id号值 26 if($id>0) 27 { 28 echo "<h3>新闻信息添加成功!</h3>"; 29 }else 30 { 31 echo "<h3>新闻信息添加失败!</h3>"; 32 } 33 echo "<a href='javascript:window.history.back();'>返回</a> "; 34 echo "<a href='index.php'>浏览新闻</a>"; 35 break; 36 case "del": //执行删除操作 37 //1.获取要删除的id号 38 $id=$_GET['id']; 39 //2.拼装删除sql语句,并执行删除操作 40 $sql = "delete from news where id={$id}"; 41 mysql_query($sql,$link); 42 43 //3.自动跳转到浏览新闻页面 44 header("Location:index.php"); 45 break; 46 case "update": //执行添加操作 47 //1.获取要修改的信息 48 $title = $_POST['title']; 49 $keywords = $_POST['keywords']; 50 $author = $_POST['author']; 51 $content = $_POST['content']; 52 $id = $_POST['id']; 53 //2.过滤要修改的信息(省略) 54 55 //3.拼装修改sql语句,并执行修改操作 56 $sql = "update news set title='{$title}',keywords='{$keywords}',author='{$author}',content='{$content}' where id = {$id} "; 57 58 mysql_query($sql,$link); 59 //4.跳转回浏览界面 60 header("Location:index.php"); 61 break; 62 } 63 //4.关闭数据库连接 64 mysql_close($link); 65
action.php文件代码
以下为index.php文件代码(在此页面浏览新闻,并对新闻信息进行增删改操作)
1 <html> 2 <head> 3 <title>新闻管理系统</title> 4 <script type="text/javascript"> 5 function dodel(id) 6 { 7 if(confirm("确定要删除吗")) 8 { 9 window.location="action.php?action=del&id="+id; 10 } 11 } 12 </script> 13 </head> 14 <body> 15 <center> 16 <?php include("menu.php");//导入导航栏 ?> 17 18 <h3>浏览新闻</h3> 19 <table width="800" border="1"> 20 <tr> 21 <th>新闻id</th> 22 <th>新闻标题</th> 23 <th>关键字</th> 24 <th>作者</th> 25 <th>发布时间</th> 26 <th>新闻内容</th> 27 <th>操作</th> 28 </tr> 29 <?php 30 //1.导入配置文件 31 require("dbconfig.php"); 32 //2.连接MYSQL,选择数据库 33 $link = @mysql_connect(HOST,USER,PASS) or die("数据库连接失败!"); 34 mysql_select_db(DBNAME,$link); 35 //3.执行查询,并返回结果集 36 $sql = "select * from news order by addtime desc"; 37 $result = mysql_query($sql,$link); 38 39 //4.解析结果集,并遍历 40 while($row = mysql_fetch_assoc($result)) 41 { 42 echo "<tr>"; 43 echo "<td>{$row['id']}</td>"; 44 echo "<td>{$row['title']}</td>"; 45 echo "<td>{$row['keywords']}</td>"; 46 echo "<td>{$row['author']}</td>"; 47 echo "<td>".date("Y-m-d",$row['addtime'])."</td>"; 48 echo "<td>{$row['content']}</td>"; 49 echo "<td> 50 <a href='javascript:dodel({$row['id']})'>删除</a> 51 <a href='edit.php?id={$row['id']}'>修改</a></td>"; 52 echo "</tr>"; 53 } 54 55 //5.释放结果集 56 mysql_free_result($result); 57 mysql_close($link); 58 ?> 59 </table> 60 </center> 61 </body> 62 </html>
index.php文件代码
以下为edit.php文件代码(编辑具体代码)
1 <html> 2 <head> 3 <title>新闻管理系统</title> 4 </head> 5 <body> 6 <center> 7 <?php 8 include("menu.php");//导入导航栏 9 10 //1.导入配置文件 11 require("dbconfig.php"); 12 13 //2.连接MYSQL数据库、选择数据库 14 $link = @mysql_connect(HOST,USER,PASS) or die("数据库连接失败!"); 15 mysql_select_db(DBNAME,$link); 16 //3.获取要修改信息的id号,并拼装查看sql语句,执行查询,获取要修改的信息 17 $sql = "select *from news where id={$_GET['id']}"; 18 $result = mysql_query($sql,$link); 19 //4.判断是否获取到了要修改的信息 20 if($result &&mysql_num_rows($result)>0) 21 { 22 $news = mysql_fetch_assoc($result); 23 }else 24 { 25 die("没有找到要修改的信息!"); 26 } 27 28 ?> 29 30 <h3>编辑新闻</h3> 31 <form action = "action.php?action=update" method="post"> 32 <input type="hidden" name="id" value="<?php echo $news['id']; ?>" /> 33 <table width="320" border="1"> 34 <tr> 35 <td align="right">标题:</td> 36 <td><input type="text" name="title" value="<?php echo $news['title']; ?>" /></td> 37 </tr> 38 <tr> 39 <td align="right">关键字:</td> 40 <td><input type="text" name="keywords" value="<?php echo $news['keywords']; ?>" /></td> 41 </tr> 42 <tr> 43 <td align="right">作者:</td> 44 <td><input type="text" name="author" value="<?php echo $news['author']; ?>" /></td> 45 </tr> 46 <tr> 47 <td align="right" valign="top">内容:</td> 48 <td><textarea cols="25" rows="5" name="content"><?php echo $news['content']; ?></textarea></td> 49 </tr> 50 <tr> 51 <td colspan="2" align="center"> 52 <input type="submit" value="编辑"/> 53 <input type="reset" value="重置"/> 54 55 </td> 56 </tr> 57 </table> 58 </form> 59 </center> 60 </body> 61 </html>
edit.php文件代码
转载于:https://www.cnblogs.com/guaidaodark/p/4467484.html
PHP基础示例:用PHP+Mysql编写简易新闻管理系统相关推荐
- 新闻管理系统源码mysql_PHP基础示例:用PHP+Mysql编写简易新闻管理系统[转]
实现目标:使用php和mysql操作函数实现一个新闻信息的发布.浏览.修改和删除操作 实现步骤: 一.创建数据库和表 1.创建数据库和表:newsdb 2.创建表格:news 字段:新闻id,标题,关 ...
- Java web+MySQL编写简易候选人投票(完整代码)
Java web+MySQL编写简易候选人投票(完整代码) 代码实现效果,自己选的图片太大了,没有做更改,将就看就行 项目用到的三个jar包自己导入就行 代码结构如下图 Candidate中的代码 p ...
- C语言编写简易图书管理系统
#include<stdio.h> #include<stdlib.h> #include<string.h> struct link *AppendNode(st ...
- 基于JAVA+SpringBoot+Mybatis+MYSQL的校园新闻管理系统
项目功能: 普通用户的注册.登录,新闻资讯信息的编辑.修改与发布,新闻详情浏览.点赞,以及管理员对新闻资讯信息监管.完善与审核等一系列操作 页面效果:
- Mybatis3基础示例
Mybatis3基础示例 一.搭建流程 总体项目结构: 1.Maven依赖 2.配置文件 Mybatis-config.xml 3.封装MybatisUtils工具类 4.其他 二.CURD 1.接口 ...
- flutter插件进阶之手把手教你编写简易插件(五)
经过前面几篇文章中对flutter插件相关技术的介绍,本篇我们从1开始手把手的进行插件代码的编写工作,以实现一个简单的桌面小部件功能. 来吧,效果展示来一波~~ 目录: 五.编写简易插件(安卓桌面小部 ...
- Mysql编写定时任务事件
Mysql编写定时任务事件 原文:Mysql编写定时任务事件 场景: 例如:某系统,用户每天只能拥有一次的抽奖机会,抽过后当天就不可再抽,但是过了24:00点后用户就重新拥有一次抽奖机会.像这种需要数 ...
- PHP+MySQL编写人事公司员工工资管理系统
PHP+MySQL编写人事公司员工工资管理系统(含源码分析) 公司人事会有很多事情要做,有些事情还需要留下记录,为了方便公司管理,人事操作简便,领导查看数据方便,本次开发了公司员工工资管理系统. 包含 ...
- Cocos2d-x 基础示例-精灵移动2
Cocos2d-x 基础示例-精灵移动2 先推荐一下红哥的精灵教程 http://blog.csdn.net/honghaier/article/details/8117903 红哥里面使用的ccTo ...
最新文章
- python使用sklearn的PrecisionRecallDisplay来可视化PR曲线
- 十大最佳Ajax教程(图文)
- P5357 【模板】AC自动机(二次加强版)
- BZOJ3916 [Baltic2014]friends
- WinXP下 扫雷程序逆向分析 --扫雷辅助(一)
- 零基础学UI设计,正确的学习方法讲解!
- Spring IOC学习心得之Bean对IOC容器的感知
- 很多人都对3dB带宽有一定误解
- Combining Label Propagation and Simple Models Out-performs Graph Neural Networks 论文理解
- 交叉编译linux内核实例(最详细)总结
- Font Awesome html源码,如何将 Font Awesome 转成 PNG 图标 详细教程 含源代码_html/css_WEB-ITnose...
- 都是做游戏,为嘛国内外出来的产品相差这么大?
- 热点分析 (Getis-Ord Gi*) 的工作原理
- 从键盘任意输入一个4位数n c语言,c语言编程题
- asp.net976-校园论坛系统的设计与实现#毕业设计
- access如何查询两张表的内容_Access使用技术2:使用交叉表查询向导进行每日汇总...
- 【IEEE会议-EI稳定检索】2022年自动化机器人与计算机工程国际学术会议(ICARCE2022)
- Word 模板渲染引擎-Poi-tl - 标签(二)
- 3G无线上网分析、优惠以及推荐选择
- 移动端H5常见问题以及解决方案