zoukankan      html  css  js  c++  java
  • 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="添加"/>&nbsp;&nbsp;
    31                             <input type="reset" value="重置"/>
    32                             
    33                             </td>
    34                     </tr>
    35                 </table>
    36             </form>
    37         </center>
    38     </body>
    39 </html>
    复制代码

    以下为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>&nbsp;&nbsp;";
    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     
    复制代码

    以下为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>
    复制代码

    以下为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="编辑"/>&nbsp;&nbsp;
    53                             <input type="reset" value="重置"/>
    54                             
    55                             </td>
    56                     </tr>
    57                 </table>
    58             </form>
    59         </center>
    60     </body>
    61 </html>
    复制代码
  • 相关阅读:
    简单探讨spring整合mybatis时sqlSession不需要释放关闭的问题
    男人常做俯卧撑,谁还敢说不行!
    五大穴位
    实战c++中的vector系列--vector的一些异常
    [Canvas画图] 藏图阁(16) 人体穴位
    算法题:剔除字符串(非常有意思)
    怎样在win8系统下建立wifi热点
    【code】flex_进度条样式
    使用bbed改动数据
    Go语言核心之美 3.2-slice切片
  • 原文地址:https://www.cnblogs.com/bluedy1229/p/4528849.html
Copyright © 2011-2022 走看看