秀野堂后台开发小记

阅读数:684 发布时间:2016-06-02 08:41:36

作者:zzl005 标签: 朱忠来005 api php

任务要求

通过后台,修改学员信息,并且在学员页面中展示出来

任务分解

  1. 前端方面

    • 学员页面
    • 后台用户管理面板
  2. 后端方面

    • 修改相关数据库,添加多个字段
    • 能用前台修改后台数据
  3. 服务器端
    • 布署代码
    • 修改数据库

具体施工

  1. 先是切图(学员页面,包含学员笔记列表,该学员的各种信息)

    由师兄提供了设计图,做了简单的响应式设计

  2. 后端修改数据库 先是在堂主机器,进行操作,经过验证,没有错误之后,再到服务器端进行修改

  3. 后端编写 PHP
    1. 一是操作数据库
    2. 二是在学员页面显示数据

具体代码

学员页面文件:

xyt/web/student.php

页头和页尾都是公共的部分(直接引入):

require_once(__SYSTEM_URL__ . "web/include/public_head.php");
require_once(__SYSTEM_URL__ . "web/include/foot.php");

其中 public_head.php 多个基类还有一个 url_router.php,用来引入正确的 css 文件,还有正确的 title 和 keyword

后台数据库

登录调试环境的 mysql,堂主本机
mysql -uroot -h192.168.0.105

直接修改 xyt_db 数据库下的 admin 表:

先查看已有字段--desc admin,发现已有四个字段: id,name,password,level,其中 id 字段是主键自增字段。

alter table  admin add  points  int(3) not null;
alter table  admin add  skills  varchar(300) not null;
alter table  admin add  projects  varchar(300) not null;
alter table  admin add  number  int(3) not null;
alter table  admin add  profile  varchar(500) not null;
alter table  admin add  keyword   varchar(200) not null;
alter table  admin add  time   datetime not null;
alter table  admin add  picture   varchar(200) not null;
alter table  admin add  graduatetime   datetime not null;
alter table  admin add  pageview   int(6) not null;
alter table  admin add  graduate   varchar(20) not null;
alter table  admin add  english   int(1) not null;
alter table  admin add  age   int(2) not null;
alter table  admin add  height   int(3) not null;
alter table  admin add  weight   int(3) not null;
alter table  admin add  school   varchar(50) not null;
alter table  admin add  region   varchar(50) not null;
alter table admin add realname varchar(8) not null;

5月30日修改:

alter table admin change english english varchar(10)  not null;  
学员路由和学员基类

定义 student_route.php 学员路由 和 class_student_info.php 学员基类

其中 student_route.php 学员路由

<?php 
require_once('../config.php');
require_once('../public/class_base.php');
require_once('../class/class_student_info.php');

$method = $_REQUEST['method'];
switch ($method) {

    /**
     *学员路由
    **/
    case 'get_student':
        echo json_encode(Student::get_student($_REQUEST['id']));
        break;
case 'insert_student':
        $params               = array();
        $params['name']       = $_REQUEST['name'];
        $params['password']   = $_REQUEST['password'];
        $params['level']   = $_REQUEST['level'];
        $params['points']   = $_REQUEST['points'];
        $params['skills']   = $_REQUEST['skills'];
        $params['projects']   = $_REQUEST['projects'];
        $params['number']   = $_REQUEST['number'];
        $params['profile']   = $_REQUEST['profile'];
        $params['keyword']   = $_REQUEST['keyword'];
        $params['time']   = $_REQUEST['time'];
        $params['picture']   = $_REQUEST['picture'];
        $params['graduatetime']   = $_REQUEST['graduatetime'];
        $params['pageview']   = $_REQUEST['pageview'];
        $params['graduate']   = $_REQUEST['graduate'];
        $params['enslish']   = $_REQUEST['enslish'];
        $params['age']  = $_REQUEST['age'];
        $params['height'] = $_REQUEST['height'];
        $params['weight']   = $_REQUEST['weight'];
        $params['school']    = $_REQUEST['school'];
        $params['region']    = $_REQUEST['region'];
        $params['realname']    = $_REQUEST['realname'];
        echo json_encode(Student::insert_student($params));
        break;

    case 'delete_student':
        echo json_encode(Student::delete_student($_REQUEST['id']));
        break;

    case 'update_student':

        ......

 ?>

调用的时候,只要传入方法名就可以。

其中 class_student_info.php 学员基类

<?php 
/**
* 学生类
*/
class Student extends Base
{

    ......

    public static function get_student($id)
    {
            $sql              = "select * from admin where id='".$id."' ";
            $rs               = Base::getSingleRs($sql);
            $result['data']   = $rs;
            $result['msg']    = '';
            $result['status'] = 1;
            return $result;
    }

    public static function insert_student($params)
    {
        $sql      = "insert into admin (name,password, level, points, skills, projects, number, profile, keyword, time, picture, graduatetime, pageview, graduate, english, age, height, weight, school, region, realname) values ('".$params['name']."', '".$params['password']."', '".$params['level']."', '".$params['points']."', '".$params['skills']."', '".$params['projects']."', '".$params['number']."', '".$params['profile']."', '".$params['keyword']."', '".$params['time']."', '".$params['picture']."', '".$params['graduatetime']."', '".$params['pageview']."', '".$params['graduate']."', '".$params['english']."', '".$params['age']."', '".$params['height']."', '".$params['weight']."', '".$params['school']."', '".$params['region']."', '".$params['realname']."')";
        $feedback = Base::addWithFeedBack($sql);

        if ($feedback > 0) {
            $result['msg']    = '成功';
            $result['status'] = 1;
            $result['data']   = $feedback;
        }
        else {
            $result['msg']    = '失败';
            $result['status'] = 0;
            $result['data']   = $sql;
        }
        return $result;
    }

    public static function update_student($params)
    {
        $update_student  = "
            name         = '".$params['name']."', 
            password     = '".$params['password']."', 
            level        = '".$params['level']."', 
            points       = '".$params['points']."', 
            skills       = '".$params['skills']."',
            projects     = '".$params['projects']."',
            number       = '".$params['number']."', 
            profile      = '".$params['profile']."',
            keyword      = '".$params['keyword']."',
            time         = '".$params['time']."',
            picture      = '".$params['picture']."',
            graduatetime = '".$params['graduatetime']."',
            pageview     = '".$params['pageview']."',
            graduate     = '".$params['graduate']."',
            english      = '".$params['english']."',
            age          = '".$params['age']."',
            height       = '".$params['height']."',
            weight       = '".$params['weight']."',
            school       = '".$params['school']."',
            region       = '".$params['region']."'
            realname     = '".$params['realname']."'
            where id = '".$params['id']."'";
        $sql         = "update admin set ".$update_student;
        $feedback    = Base::modiWithFeedBack($sql);
        if ($feedback > 0) {
            $result['msg']    = '成功';
            $result['status'] = 1;
            $result['data']   = $feedback;
        }
        else {
            $result['msg']    = '失败';
            $result['status'] = 0;
            $result['data']   = $sql;
        }
        return $result;
    }

    public static function delete_student($id)
    {
        $delete_student = "id = $id";
        $sql        = "delete from admin where ".$delete_student;
        $feedback   = Base::modiWithFeedBack($sql);
        if ($feedback > 0) {
            $result['msg']    = '成功';
            $result['status'] = 1;
            $result['data']   = $feedback;
        }
        else {
            $result['msg']    = '失败';
            $result['status'] = 0;
            $result['data']   = $sql;
        }
         return $result;
    }

        ......

 ?>

然后在 student.php 学员页面中
根据传入的学员姓名,如果为空,则提示--没有该学员
如果传入的学员姓名不存在,则提示--用户不存在

$username = $_REQUEST['n']; //接收name字段

    <link rel="stylesheet" type="text/css" href="<?php echo __HTTP_REQUEST_URL__."css/view.css";?>">
    <?php 
    if ($username == '') {
        echo "没有该学员";
        require_once(__SYSTEM_URL__ . "web/include/foot.php");
        die();    //如果没有传入学员字段,直接输出没有"没有该学员"
    }

    $rs_userinfo = Student::getStudentInfo($username);
    if ($rs_userinfo['status'] == 1) {
        $rs = $rs_userinfo['data'];
    }
    else{
        var_dump($rs_userinfo);
        die('用户不存在!');
    }    //如果传入学员字段不在数据库中,则提示"用户不存在"
通过页面修改数据库

之前在学员页面获取到的学员信息都是在命令行里操作数据库,很不方便,这个时候,需要提供一个在用户页面里,直接修改后台数据库的功能。

用户管理页面设计:

其实就是一个表单提交:

admin_user_modi.php中的表单

<form action="admin_user_oper.php" method="post">

            ······
    <tr>
            <td colspan="4" align="center">
                <input class= "submit" type="submit" value="修改" name="action">
            </td>
    </tr>
</form>

admin_user_oper.php

switch ($action) {
    case 'del':

            ......

            break;
    case 'hide':

            ......

            break;
    case '修改':
        $result['id']         = $_POST['id'];
        if ($_POST['password'] != '') {
            $result['password']     = ($_POST['password']);    
        } else {
            // $result['password']     = md5($_POST['password']);    
        }
        $result['level']        = $_POST['level'];
        $result['points']       = $_POST['points'];
        $result['skills']       = $_POST['skills'];
        $result['projects']     = $_POST['projects'];
        $result['number']       = $_POST['number'];
        $result['profile']      = $_POST['profile'];
        $result['keyword']      = $_POST['keyword'];
        $result['time']         = $_POST['time'];
        $result['picture']      = $_POST['picture'];
        $result['graduatetime'] = $_POST['graduatetime'];
        $result['pageview']     = $_POST['pageview'];
        $result['graduate']     = $_POST['graduate'];
        $result['english']      = $_POST['english'];
        $result['age']          = $_POST['age'];
        $result['height']       = $_POST['height'];
        $result['weight']       = $_POST['weight'];
        $result['school']       = $_POST['school'];
        $result['region']       = $_POST['region'];
        $result['realname']     = $_POST['realname'];
        $feedback = USER::editorModi($result);
        if($feedback['status'] === 0 ) {
            echo "<script>alert(".$feedback['msg'].");history.go(-1);</script>";
            exit;
        }
        echo "<script>window.location ='admin_index.php';</script>";

        break;
    case '添加用户':

            ......

        break;
    default:

            ......

        break;
}

用户管理1
用户管理2
用户管理3

在学员页面需要显示的有,学号、所积学分、技能树、参与项目、学员简介、入学时间、入学年龄、英语等级、个人简介等

在 student.php 中,只要获取到记录集的某个索引就可以了

<div >学号: <?php echo $rs['number'] ?></div>    <!--显示学号-->

        ......

<details class="profileTitle">
    <summary>技能树:</summary>
    <?php
        $skills = explode(",",$rs['skills']);
        foreach ($skills as $value) {
            echo "<li>".$value."</li>";
            }    //按照英文逗号','截取skills字段,并且通过拼接循环列表    
    ?>
</details>

学员页面1
学员页面2

服务器端布署代码

  1. 先将代码在本地测试,确认没问题后,再布署
  2. 登录服务器
  3. 切换到代码目录
  4. 拉取代码

过程中的错误总结

  1. Mysql 中一次更新多条字段

    update 表名 set  字段名 =  '".xxxx."',
                        字段名 =  '".xxxx."',
                        字段名 =  '".xxxx."',
                        字段名 =  '".xxxx."',
                        字段名 =  '".xxxx."',
                        字段名 =  '".xxxx."',
                        字段名 =  '".xxxx."',
    where id = '".xxx."';
  2. linux 查找文件命令:sudo find / -name

  3. redis 启动

  4. 通过 <td colspan="value"> 设置单元格横跨的列数

相关文章推荐: