PHP基础示例:用PHP+Mysql编写简易新闻管理系统

实现目标:使用php和mysql操作函数实现一个新闻信息的发布、浏览、修改和删除操作

实现步骤:

一、创建数据库和表

  1.创建数据库和表:newsdb

  2.创建表格:news

  字段:新闻id,标题,关键字,作者,发布时间,新闻内容

二、创建php文件编写代码(以下为要创建的php文件及其用途)

dbconfig.php 公共配置文件,数据库连接配置信息

menu.php  网站公共导航栏

index.php  浏览新闻的文件(此为首页)

add.php   发布新闻表单页

edit.php   编辑新闻的表单页

action.php  执行新闻信息添加、修改、删除等操作的动作(后台)

**********************************************************************


以下为数据库创建语句:

create database newsdb;//创建数据库语句create table news(id int unsigned not null auto_increment primary key,title varchar(64) not null,keywords varchar(64) not null,author varchar(16) not null,addtime int unsigned not null,content text not null);//创建表语句数据库创建语句


以下为dbconfig.php文件代码

<?php//公共信息配置//数据库配置信息define("HOST","localhost"); //主机名define("USER","root");      //账号define("PASS","root");      //密码define("DBNAME","newsdb");  //数据库名?>


以下为menu.php文件代码(一开始浏览的页面,添加新闻后以index页面为主)


 1 <h2>新闻管理系统</h2> 2 <a href="index.php">浏览新闻</a> 3 <a href="add.php">发布新闻</a> 4 <hr width="90%"/>


以下为add.php文件代码(增加具体代码)


<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="1">                    <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="添加"/>&nbsp;&nbsp;                            <input type="reset" value="重置"/>                                                        </td>                    </tr>                </table>            </form>        </center>    </body></html>add.php文件代码



以下为action.php文件代码(增删改实现代码)


<?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}')";                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>&nbsp;&nbsp;";                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} ";                        mysql_query($sql,$link);            //4.跳转回浏览界面            header("Location:index.php");            break;    }//4.关闭数据库连接    mysql_close($link);    action.php文件代码



以下为index.php文件代码(在此页面浏览新闻,并对新闻信息进行增删改操作)


<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="800" 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>index.php文件代码



以下为edit.php文件代码(编辑具体代码)

<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="1">                    <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="编辑"/>&nbsp;&nbsp;                            <input type="reset" value="重置"/>                                                        </td>                    </tr>                </table>            </form>        </center>    </body></html>edit.php文件代码