PHP开发入门 | 简单的PHP新闻管理系统案例

本文介绍了一个基于PHP、MySQL和Bootstrap的简易新闻管理系统,适合初学者理解新闻管理的基本流程和技术实现,包括浏览、编辑和发布新闻的功能。
PHP新闻管理系统(PHP+MySQL+bootsrap)

php学习课余,利用这段时间学的php基础写一个简单的新闻管理,功能很简单,主要用php+MySQL+Bootraps写成的简单页面,适合初学者观看,共勉!

界面效果图如下

浏览页面在这里插入图片描述
编辑页面
在这里插入图片描述

案例所包含内容:6个php文件+bootsraps.min.css+数据库

在这里插入图片描述

数据库如下:

在这里插入图片描述

menu.php
// 主页菜单文件
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<h2>新闻管理系统</h2>
<a href="index.php">浏览新闻</a>
<a href="add.php">发布新闻</a>
<hr width="80%">
</body>
</html>
database.php
<?php
define("host","localhost");
define("user","root");
define("password","");
define("dbname","newsdb");
?>
index.php
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../../css/bootstrap.min.css">
    <title>新闻管理系统</title>
    <script>
        function dodel(id){
            if (confirm("确定要删除吗")){
                window.location="action.php?action=del&id="+id;
            }
        }
    </script>
</head>
<body>
<center>
    <?php include ("menu.php");?>
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h2 class="panel-title">浏览新闻</h2>
        </div>
        <div class="panel-body form-inline">
        </div>
    </div>
    <table class="table table-bordered table-hover table-striped">
        <thead>
          <tr>
            <th>新闻id</th>
            <th>新闻标题</th>
            <th>关键字</th>
            <th>作者</th>
            <th>发布时间</th>
            <th>新闻内容</th>
            <th>操作</th>
          </tr>
        </thead>
        <?php
         require("database.php");
        $connection=@mysqli_connect(host,user,password) or die("数据库连接失败!");
        @mysqli_select_db($connection,dbname);

        //执行查询并返回结果集
        $sql="select * from news order by addtime desc";
        $result=mysqli_query($connection,$sql);

        //解析结果集 并遍历
        while($row =mysqli_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>";
        }

        //释放结果集
        mysqli_free_result($result);
        mysqli_close($connection);

        ?>
    </table>
  </center>
</body>
</html>
edit.php
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../../css/bootstrap.min.css">
    <title>新闻管理系统</title>
</head>
<body>
    <?php
      include ("menu.php");
      include("database.php");

    $connection=@mysqli_connect(host,user,password) or die("数据库连接失败!");
    @mysqli_select_db($connection,dbname);

    //获取要修改id号
    $sql="select * from news where id={$_GET['id']}";
    $result=mysqli_query($connection,$sql);

    //判断是否获取到了要修改的信息
    if($result && mysqli_num_rows($result)>0){
        $news=mysqli_fetch_assoc($result);
    }else{
        die("没有找到要修改的信息!");
    }
    ?>
    <div class="panel panel-primary">
        <div class="panel-heading">
            <h2 class="panel-title">编辑新闻</h2>
        </div>
        <div class="panel-body form-inline">
        </div>
    </div>
    <form action = "action.php?action=update" method="post">
        <input type="hidden" name="id" value="<?php echo $news['id']; ?>" />
        <table class="table table-bordered table-hover table-striped">
            <thead>
            <tr>
                <td align="right">标题:</td>
                <td><input type="text" name="title" size="100" value="<?php echo $news['title']; ?>" /></td>
            </tr>
            <tr>
                <td align="right">关键字:</td>
                <td><input type="text" name="keywords" size="100" value="<?php echo $news['keywords']; ?>" /></td>
            </tr>
            <tr>
                <td align="right">作者:</td>
                <td><input type="text" name="author" size="100" value="<?php echo $news['author']; ?>" /></td>
            </tr>
            <tr>
                <td align="right" valign="top">内容:</td>
                <td><textarea cols="100" rows="5" name="content"><?php echo $news['content']; ?></textarea></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" name="update" value="编辑"/>&nbsp;&nbsp;
                    <input type="reset" value="重置"/>
                </td>
            </tr>
            </thead>
        </table>
    </form>
 </body>
</html>
add.php
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <link rel="stylesheet" href="../../css/bootstrap.min.css">
    <title>新闻管理系统</title>
</head>
<body>
    <?php include ("menu.php");?>

    <div class="panel panel-primary">
        <div class="panel-heading">
            <h2 class="panel-title">发布新闻</h2>
        </div>
        <div class="panel-body form-inline">
        </div>
    </div>
   <div class="container">
       <form action="action.php?action=add" method="post">
           <table class="table table-bordered table-hover table-striped">
               <tr>
                   <td >标题:</td>
                   <td><input type="text" name="title" size="100"></td>
               </tr>
               <tr>
                   <td>关键字:</td>
                   <td><input type="text" name="keywords" size="100"></td>
               </tr>
               <tr>
                   <td >作者:</td>
                   <td><input type="text" name="author" size="100"></td>
               </tr>
               <tr>
                   <td >内容:</td>
                   <td><textarea name="content"  cols="100" rows="5" ></textarea></td>
               </tr>
               <tr>
                   <td align="center" colspan="2">
                       <input type="submit" name="add" value="添加">&nbsp;&nbsp;
                       <input type="reset" value="重置">
                   </td>
               </tr>
           </table>
       </form>
   </div>
</body>
</html>
action.php
<meta charset="UTF-8">
<?php
require("database.php");

//连接mysql,并选择数据库
$connection=@mysqli_connect(host,user,password) or die("数据库连接失败!");
@mysqli_select_db($connection,dbname);

switch ($_GET['action']){

    case "add"://执行添加操作
        //获取要添加的信息并补充其他信息
        $title=$_POST["title"];
        $keywords=$_POST["keywords"];
        $author=$_POST["author"];
        $content=$_POST["content"];
        $addtime=time();

        $sql="insert into news values (null,'{$title}','{$keywords}','{$author}','{$addtime}','{$content}')";
        @mysqli_query($connection,$sql);

        //判断是否成功
        $id=mysqli_insert_id($connection);//获取刚刚添加信息的自增id值
        if($id>0){
             echo "新闻信息添加成功!";
        }else{
             echo "新闻信息添加失败!";
        }
        echo "<a href='javascript:window.history.back();'>返回</a>&nbsp;&nbsp;";
        echo "<a href='index.php'>浏览新闻</a>";
     break;
    case "del"://执行删除操作
        $id=$_GET['id']; //获取亚删除的id号
        $sql="delete from news where id={$id}";
        mysqli_query($sql,$connection);//执行删除操作

        //自动跳转到浏览新闻页面
        header("Location:index.php");
     break;
    case "update":
        $title=$_POST['title'];
        $keywords=$_POST['keywords'];
        $author=$_POST['author'];
        $content=$_POST['content'];
        $id=$_POST['id'];
        $sql="update news set title='{$title}',keywords='{$keywords}',author='{$author}',content='{$content}' where id={$id}";

        mysqli_query($connection,$sql);
        header("Location:index.php");//跳回浏览界面
    break;
}

//关闭数据库连接
mysqli_close($connection);

目前这个只是初步效果,功能没有完善全,后面再补上…

网站是基于PHPmysql数据库架设 网站功能介绍: 一.登陆界面 1.用户登录:网页打开出现用户登录界面,如果已拥有帐号可进行用户登录操作 2.用户注册:在没有帐号的情况下可点击'立即注册'进入注册页面,按说明填写后便可注册成功,如果注册失败系统会提示出错的地 方 3.忘记密码:在忘记密码的情况下可根据用户名和E-MAIL地址提示用户注册时的安全问题,如正确回答问题便可找回密码 二.主页面 主页面进入后左侧显示连接,右侧页面显示最新发布的5条新闻和最新上传的5张照片,点击'更多'便能查看全部内容,内容实现分页功能 1.新闻系统:新闻分为添加和管理,添加新闻时可以添加图片,管理则可对新闻进行修改和删除,修改新闻能对其图片进行修改,并把原有图片删除 2.图片系统:图片分为添加和管理,添加图片时可以点击'预览',在网页下方生成该图片的预览效果,方便用户查看,管理则可对图片进行修改和删除,修改只能改图片的标题和说明,如对图片不满意就直接删除 3.留言板:基于上述两项功能又增加了留言板,其功能与上述两项大同小异,主要也是添加与管理,并具有浏览,能查看用户的留言,按用户留言的时间排序实现分页 /*上述三项功能主要实现对数据的增删改查*/ 4.投票系统:该功能主要是调查用户对本网站的满意程度,并反馈信息以便日后完善 5.友情链接:连接国内热门的门户网站,方便用户转站
评论 7
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值