前端小白之每天学习记录----php(8)message留言板

什么样子的留言板?

它包含三个页面

1.添加留言页面

像这样:它的作用是当你输入标题与内容按添加后,把题目与内容保存进数据库

 

2.信息列表页面

它的作用是从数据库提取留言,显示留言的id与标题,并添加了操作按钮,可以修改和删除

 

3.修改页面

它的作用是修改标题与内容

 

代码:

1.添加留言页面(新建msg_add.php)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .my-form {
            margin-top: 60px;
        }
    </style>
</head>
<body>
    <!-- 这是添加页面 -->
    <div class="container">
        <div class="row">
            <!-- 一个跳转到信息列表页面的链接 -->
            <a href="msg_list.php" class="btn btn-link">信息列表</a>
            <div class="col-md-offset-3 col-md-6">
                                <!-- 定义一个表单 用post方式提交信息 -->
                <form action="" class="form-horizontal my-form" method="post">
                    <div class="form-group">
                        <label for="" class="control-label col-md-2">标题:</label>
                        <div class="col-md-8">
                            <!-- 第一个提交的信息$_POST['title'] -->
                            <input type="text" name="title" class="form-control">
                        </div> 
                    </div>
                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-8">
                            <!-- 第二个提交的信息$_POST['content']-->
                            <textarea name="content"  class="form-control" id="" cols="10" rows="5"></textarea>
                        </div> 
                    </div>
                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-8">
                            <!-- 提交按钮 -->
                            <button type="submit" class="btn btn-primary">发布/添加</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <?php
        require("../lib/mysql.class.php"); //应用封装的数据库连接 请看php6 面向对象
    ?>

    <?php
        if( !empty( $_POST['title'] ) && !empty( $_POST['content'] ) ){ //表单提交的信息非空
            $title = $_POST['title'];
            $content=  $_POST['content'];
            $sql = "INSERT INTO message( title, content ) VALUES( '$title', '$content' )";
              //向message表单里面插入题目和内容的语句          
            $res = mysql_query( $sql );  //执行语句 (在mysql.class.php封装的方法))
            if( $res !== false ){
                echo "<script>alert('留言发布成功');</script>";
            }else {
                echo "<script>alert('留言发布失败');</script>";
            }
        }
    ?>

 

</body>
</html>

2.信息列表页面(新建msg_list.php)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
   <?php 
        require("../lib/mysql.class.php");//应用封装的数据库连接 请看php(6) 面向对象
?>

    <?php
        if( isset( $_GET['act'] ) && $_GET['act'] == 'del' ){ //是否点击了删除按钮
            $sql = "DELETE FROM message WHERE msg_id = " . $_GET['msg_id']; //根据id删除相应数据
            $res = $mysql->query( $sql );//执行删除语句
            if( $res !== false ) { 
                echo '<script>alert("删除成功");</script>';
                echo "<script>location.href='msg_list.php';</script>";//删除成功刷新页面
            }
        }
        $msgList = $mysql->getAll( "SELECT * FROM message ORDER BY msg_id ASC" );//获取数据保存在$msgList变量里
    ?>

    <div class="container">
        <div class="row">
            <a href="msg_add.php">添加留言</a>
            <table class="table table-bordered table-hover table-striped">
                <tr>
                    <th>信息id</th>
                    <th>标题</th>
                    <th>操作</th>
                </tr>
                <?php
                    foreach( $msgList as $k => $v ){  //遍历数据 遍历开始 
?>
                    <tr>
                        <td><?php echo $v['msg_id']; ?></td> 
                        <td><?php echo $v['title']; ?></td>
                        <td>
                            <a href="msg_edit.php?act=edit&id=<?php echo $v['msg_id'];//跳到编辑页面,并提交act=edit编辑信息和id信息 ?>">编辑/修改</a>
                            <a href="?act=del&msg_id=<?php echo $v['msg_id']; //提交删除信息,与需要删除的留言id,刷新本页面?>">删除</a>
                        </td>
                    </tr>
<?php
                    }    //遍历结束
                ?>
            </table>
        </div>
    </div>

</body>
</html>

 3.修改留言页面(新建msg_edit.php) 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .my-form {
            margin-top: 60px;
        }
    </style>
</head>
<body>
    <?php
        require('../lib/mysql.class.php'); //应用封装的数据库连接 请看php6 面向对象
        $msgInfo = array(); //定义一个数组

        if( isset( $_POST['update'] ) ){  //接收更新信息(点击了更新按钮)
            $title = $_POST['title']; 
            $content = $_POST['content'];
            $res = $mysql->update( array(  //运用mysql.class.php封装的更新方法更新数据库数据
                'title' => $title,     
                'content' => $content,
            ), "message", " WHERE msg_id = " . $_GET['id'] );  //在message表里面的对应msg_id更新title和content
            if( $res !== false ){
                echo '<script>alert("更新成功");</script>';
            }else {
                echo '<script>alert("更新失败");</script>';
            }
        }

        if( isset( $_GET['act'] ) && $_GET['act'] == 'edit' ){  // 检测是否有编辑信息
            if( !empty(  $_GET['id'] ) ){    //检测id是否为空
                $msgInfo = $mysql->getRow( "SELECT * FROM message WHERE msg_id = " . $_GET['id'] );//根据id查询信息
            }
        }
?>

    <div class="container">
        <div class="row">
            <div class="col-md-offset-3 col-md-6">
                <a class="btn btn-link" href="msg_list.php">信息列表</a>
                <!-- 定义一个表单 用post方式提交信息到本页面,刷新本页面 -->
                <form action="" class="form-horizontal my-form" method="post">
                    <div class="form-group">
                        <label for="" class="control-label col-md-2">标题:</label>
                        <div class="col-md-8">
                            <!-- 根据id查询的信息填充标题 -->
<input type="text" name="title" class="form-control" value="<?php echo !empty( $msgInfo['title'] ) ? $msgInfo['title'] : '';?>">
                        </div> 
                    </div>
                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-8">
                            <textarea name="content"  class="form-control" id="" cols="10" rows="5">
                                <?php echo !empty($msgInfo['content']) ? $msgInfo['content'] : ''; //根据id查询的信息填充留言内容?>
                            </textarea>
                        </div> 
                    </div>
                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-8">
                            <!-- 点击修改提交信息 $_POST['update']-->
                            <button type="submit" name="update" class="btn btn-primary">更新/修改</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>


</body>
</html>
posted @ 2017-08-19 12:46  沃迪森啊  阅读(226)  评论(0编辑  收藏  举报