zoukankan      html  css  js  c++  java
  • 【PHP小项目使用MVC架构】

    小项目名称是雇员管理系统。

    mvc是一种项目的开发模式,中文名称为模式视图控制器,是强制程序员将数据的输入、处理、输出分开的一种开发模式。

    在这个小项目中,控制器使用service作为后缀名。

    项目uml图解概述:

    在此之前,需要先创建数据库empmanage,同时创建两张表,一张表为admin,令一张表为emp,创建admin表的sql语句:

    create table admin
    (
    id int primary key,
    name varchar(32) not null,
    password varchar(64) not null
    );
    
    create table emp(
    id int primary key auto_increment,
    name varchar(64) not null,
    grade  tinyint /****1表示1级工人,2表示2级工人*****/,
    email varchar(64) not null,
    salary float
    );
    View Code

    按照字段名向admin表插入1条管理员用户数据,向emp表插入几万条数据即可,向emp表插入数据的时候使用mysql的蠕虫复制功能,分分钟就可以搞定。

    将root密码改为5a6f38,否则连接不到数据库。

    下面将会将文件名以及文件中的源代码介绍给大家。

    login.php

     1 <?php
     2 require_once 'common.php';
     3 ?>
     4 <html>
     5 <head>
     6 <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
     7 </head>
     8 <!--此版本完成了验证码验证的问题-->
     9 
    10 <body>
    11 <h1>管理员登陆界面</h1>
    12 <form action="loginProcess.php" method="post">
    13 <table>
    14 <tr>
    15 <td>请输入用户id:</td>
    16 <td><input type="text" name="admin_id" value="<?php echo getUserName();?>"/></td>
    17 </tr>
    18 
    19 <tr>
    20 <td>请输入用户密码:</td>
    21 <td><input type="password" name="admin_password" value="<?php echo getPassword();?>"/></td>
    22 </tr>
    23 
    24 <tr>
    25 <td>请输入验证码:</td>
    26 <td><input type="text" name="checkCode"/></td>
    27 <td><img src="checkCode.php" onclick="this.src='checkCode.php?re='+Math.random()"/></td>
    28 </tr>
    29 <tr>
    30 <td colspan="2">
    31 两周内免登陆本系统:
    32 <input type="checkbox" name="userinf" value="on" checked="checked"/>
    33 </td>
    34 </tr>
    35 
    36 <tr>
    37 <td><input type="submit" value="单击登陆"/></td>
    38 <td><input type="reset" value="重新填写"/></td>
    39 </tr>
    40 </table>
    41 </form>
    42 
    43 <?php
    44 $error="初始值";
    45 $error=$_GET['error'];//使用header传递参数,默认的提交方式是GET
    46 echo "<font color=red>".$error."</font>";
    47 ?>
    48 </body>
    49 </html>
    View Code

    addEmp.php

     1 <?php
     2 /**
     3  * 进行用户验证
     4  */
     5 require_once 'common.php';
     6 checkUser(); 
     7 ?>
     8 
     9 
    10 <html>
    11 <!--这个是专门针对添加用户设计的界面-->
    12 <head>
    13 <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    14 </head>
    15 <body>
    16 <center><h1><font size='30px'>这里是添加用户的界面</font></h1></center>
    17 <br/>
    18 <br/>
    19 <form action="empProcess.php" method="post">
    20 <table align="center" cellpadding="10px">
    21 
    22 <tr align="center">
    23 <td>请输入姓名:</td>
    24 <td><input type="text" name="name"/></td>
    25 </tr>
    26 
    27 <tr align="center">
    28 <td>请输入您的等级:</td>
    29 <td><input type="text" name="grade"/></td>
    30 </tr>
    31 
    32 <tr align="center">
    33 <td>请输入您的邮箱:</td>
    34 <td><input type="text" name="email"/></td>
    35 </tr>
    36 
    37 <tr align="center">
    38 <td>请输入您的薪水:</td>
    39 <td><input type="text" name="salary"/></td>
    40 </tr>
    41 <input type="hidden" name="type" value="add"/>
    42 <tr align="center">
    43 <td><input type="submit" name="submit" value="单击提交" /></td>
    44 <td><input type="reset" name="reset" value="重新输入" /></td>
    45 </tr>
    46 <tr>
    47 <td colspan="2"><a href="empMain.php?name=admin">单击返回上一级</a></td>
    48 </tr>
    49 </table>
    50 </form>
    51 </body>
    52 </html>
    View Code

    Admin.class.php

     1 <?php
     2 class Admin 
     3 {
     4     private $id;
     5     private $name;
     6     private $password;
     7     public function getid()
     8     {
     9         return $this->id;
    10     }
    11     public function setid($id)
    12     {
    13         $this->id=id;
    14     }
    15     public function getname()
    16     {
    17         return $this->name;
    18     }
    19     public function setname($name)
    20     {
    21         $this->name=$name;
    22     }
    23     public function getpassword()
    24     {
    25         return $this->password;
    26     }
    27     public function setpassword($password)
    28     {
    29         $this->password=$password;
    30     }
    31 }
    32 ?>
    View Code

    AdminService.class.php

     1 <?php
     2 require_once 'SqlHelper.class.php';
     3 class AdminService
     4 {
     5     public function checkAdmin($get_id,$get_password)
     6     {
     7         $sql="select password from admin where id='".$get_id."'";
     8         //建立一个SqlHelper.class.php对象
     9         $sqlhelper=new SqlHelper("localhost","root","5a6f38","empmanage");
    10         $res=$sqlhelper->dql($sql);
    11         $row=mysql_fetch_assoc($res);
    12         mysql_free_result($res);
    13         mysql_close($sqlhelper->conn);
    14         if($row)
    15         {
    16             $password=$row['password'];
    17             if(md5($get_password)==$password)
    18             {
    19                 return 1;
    20             }
    21             return 0;
    22         }
    23         return 0;
    24     }
    25     public function getName($id)
    26     {
    27         $sqlhelper=new SqlHelper("localhost","root","5a6f38","empmanage");
    28         $sql="select name from admin where id='".$id."'";
    29         $res=$sqlhelper->dql($sql);
    30         $row=mysql_fetch_assoc($res);
    31         if($row)
    32         {
    33             mysql_free_result($res);
    34             mysql_close($sqlhelper->conn);
    35             return $row['name'];
    36         }
    37         die("查无此人!");
    38     }
    39 }
    40 ?>
    View Code

    checkCode.php

     1 <?php
     2     //首先定义一个空字符串
     3     $checkCode="";
     4     //随机生成四个数并拼接起来
     5     for($i=1;$i<=4;$i++)
     6     {
     7         $checkCode.=rand(0,9);
     8     }
     9     session_start();
    10     $_SESSION['checkCode']=$checkCode;
    11     //开始绘制验证码
    12 
    13     //1.生成画布
    14     $im=imagecreatetruecolor(45,25);
    15     //2.随机生成一个颜色
    16     $color=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
    17     //$color=imagecolorallocate($im,255,0,0);
    18 
    19     //3.绘制干扰线
    20     for($i=1;$i<=20;$i++)
    21     {
    22         imageline($im,0,rand(0,24),44,rand(0,24),imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255)));
    23     }
    24     //4.绘制字符串
    25     imagestring($im,5,3,3,$checkCode,$color);
    26     header("content-type: image/png");
    27     imagepng($im);
    28 
    29     //4.销毁图片
    30     imagedestroy($im);
    31 ?>
    View Code

    common.php

     1 <?php
     2 /**
     3  * 这个方法专门用于用户判断
     4  */
     5 function checkUser()
     6 {
     7     session_start();
     8     if(empty($_SESSION['name']))
     9     {
    10         header("Location:login.php?error=请先登录在进行其他操作!");
    11         exit();
    12     }
    13 }
    14 /**
    15  * 这个类中专门存放公共的方法,如取cookie等行为
    16  */
    17 function getUserName()
    18 {
    19     if(!empty($_COOKIE['name']))
    20     {
    21         return $_COOKIE['name'];
    22     }    
    23     else 
    24     {
    25         return "";
    26     }
    27 }
    28 function getPassword()
    29 {
    30     if(!empty($_COOKIE['password']))
    31     {
    32         return $_COOKIE['password'];
    33     }
    34     else
    35     {
    36         return "";
    37     }
    38 }
    39 function getLastTime()
    40 {
    41     date_default_timezone_set("Asia/Chongqing");
    42     if(empty($_COOKIE['lasttime']))
    43     {
    44         setCookie("lasttime",date("Y年m月d日 H时m分s秒"),Time()+14*24*3600);
    45         echo  "您是第一次登陆本系统!";
    46     }
    47     else 
    48     {
    49         setCookie("lasttime",date("Y年m月d日 H时m分s秒"),Time()+14*24*3600);
    50         echo "您上一次的登录时间是:".$_COOKIE['lasttime'];
    51     }
    52 }
    53 ?>
    View Code

    Emp.class.php

     1 <?php
     2 class Emp
     3 {
     4     private $id;
     5     private $name;
     6     private $grade;
     7     private $email;
     8     private $salary;
     9     public function setid($id)
    10     {
    11         $this->id=$id;
    12     }
    13     public function getid()
    14     {
    15         return $this->id;
    16     }
    17     public function setname($name)
    18     {
    19         $this->name=$name;
    20     }
    21     public function getname()
    22     {
    23         return $this->name;
    24     }
    25     public function setgrade($grade)
    26     {
    27         $this->grade=$grade;
    28     }
    29     public function getgrade()
    30     {
    31         return $this->grade;
    32     }
    33     public function setemail($email)
    34     {
    35         $this->email=$email;
    36     }
    37     public function getemail()
    38     {
    39         return $this->email;
    40     }
    41     public function setsalary($salary)
    42     {
    43         $this->salary=$salary;
    44     }
    45     public function getsalary()
    46     {
    47         return $this->salary;
    48     }
    49 }
    50 ?>
    View Code

    empList.php

      1 <?php
      2 /**
      3  * 进行用户验证
      4  */
      5 require_once 'common.php';
      6 checkUser(); 
      7 ?>
      8 <html>
      9 <head>
     10 <script type="text/javascript">
     11 function confirmDel(val)
     12 {
     13     return window.confirm("确认删除"+val+"号雇员吗?");
     14 }
     15 </script>
     16 </head>
     17 <body>
     18 <?php
     19     require_once 'EmpService.class.php';
     20     //本子功能主要知识点:分页管理功能,可以使用死去活来法逐步实现。
     21     echo "<center>";
     22     echo "<h1>用户管理</h1><br/>";
     23     $empservice=new EmpService();
     24     //获取emp表的记录数。
     25     $rowCount=$empservice->getRowCount();
     26     //设置pageNow的默认值。
     27     $pageNow=1;
     28     //获取pageNow的值
     29     if($_GET['pageNow'])
     30     {
     31         $pageNow=$_GET['pageNow'];
     32     }
     33     //设置$pageSize的默认值,可以由用户自定义
     34     $pageSize=7;
     35     //计算pageCount的值
     36     $pageCount=ceil($rowCount/$pageSize);
     37     if($pageNow<=0)
     38     {
     39         echo "访问出现上溢,自动跳转到第一页!<br/><br/>";
     40         $pageNow=1;
     41     }
     42     else if($pageNow>=($pageCount+1)) 
     43     {
     44         echo "访问出现下溢,自动跳转到最后一页!<br/><br/>";
     45         $pageNow=$pageCount;
     46     }
     47     $arr_res=$empservice->getResource($pageNow, $pageSize);
     48     $rows=mysql_affected_rows($empservice->getSqlHelper()->conn);
     49     $cols=mysql_num_fields($empservice->getResource_unArray());
     50     
     51 
     52     echo "<table bgcolor=black rows=".($rows+1)." cols=".($cols+2)." cellspacing=1px cellpadding=10px>";
     53     echo "<tr bgcolor=white>";
     54     for($i=0;$i<$cols;$i++)
     55     {
     56         echo "<th>".mysql_field_name($empservice->getResource_unArray(),$i)."</th>";
     57     }
     58     echo "<th>修改用户信息</th>";
     59     echo "<th>删除用户</th>";
     60     echo "</tr>";
     61     for($i=0;$i<count($arr_res);$i++)
     62     {
     63         $row=$arr_res[$i];
     64         echo "<tr bgcolor=white>";
     65         foreach($row as $key=>$value)
     66         {
     67             echo "<td align=center>".$value."</td>";
     68         }
     69         echo "<td align=center><a href='updateEmpUI.php?id={$row['id']}'>修改</a></td>";
     70         echo "<td align=center><a onclick=
     71         'return confirmDel({$row['id']})' 
     72         href='empProcess.php?type=del&id={$row['id']}
     73         &pageNow={$pageNow}'>删除</a></td>";
     74         echo "</tr>";
     75     }
     76     echo "</table><br/>";
     77     $empservice->closeAll();
     78     echo "<a href='empList.php?pageNow=1'>首页</a>&nbsp;&nbsp;&nbsp;";
     79     if($pageNow>=2)
     80     {
     81         echo "<a href='empList.php?pageNow=".($pageNow-1)."'>上一页</a>&nbsp;&nbsp;&nbsp;";
     82     }
     83     else 
     84     {
     85         echo "上一页&nbsp;&nbsp;&nbsp;";
     86     }
     87     //一下将会实现6页6页的整体翻页。
     88      $index=floor(($pageNow-1)/6)*6+1;
     89      $start=$index;
     90      echo "<a href='empList.php?pageNow=".($start-1)."'><<</a>&nbsp;&nbsp;&nbsp;";
     91     for(;$start<=5+$index;$start++)
     92     {
     93         if($start<=$pageCount)
     94         {
     95             echo "<a href='empList.php?pageNow=".$start."'>[".$start."]";
     96             echo "</a>"."&nbsp;&nbsp;&nbsp;";
     97         }
     98         else 
     99         {
    100             $start--;
    101             break;
    102         }
    103     }
    104     /**
    105      * 如果有6条记录,现在是第2条,那么2/6=0.33,floor(0.33)==0,
    106      * ...什么时候打印?求打印条件。
    107      */
    108     echo "...&nbsp;&nbsp;&nbsp;";
    109     echo "[当前页{$pageNow}/共{$pageCount}页]&nbsp;&nbsp;&nbsp;";
    110     echo "<a href='empList.php?pageNow=".$start."'>>></a>&nbsp;&nbsp;&nbsp;";
    111     if($pageNow<=($pageCount-1))
    112     {
    113         echo "<a href='empList.php?pageNow=".($pageNow+1)."'>后一页</a>&nbsp;&nbsp;&nbsp;";
    114     }
    115     else 
    116     {
    117         echo "后一页&nbsp;&nbsp;&nbsp;";
    118     }
    119     echo "<a href='empList.php?pageNow=".$pageCount."'>尾页</a>";
    120     echo "<br/>";
    121 ?>
    122 <br/>
    123 <form action="empList.php" method="get">
    124 请输入跳转页:<input type="text" name="pageNow"/>
    125 <input type="submit" value="GO"/>
    126 </form>
    127 
    128 </body>
    129 </html>
    View Code

    empMain.php

     1 <?php
     2 
     3 /**
     4  * 进行用户验证
     5  */
     6 require_once 'common.php';
     7 checkUser();
     8 
     9 
    10     $admin_name=$_GET['name'];
    11     require_once 'common.php';
    12     getLastTime();
    13     echo "<center>";
    14     if(!$admin_name)
    15     {
    16         die("没有收到用户名数据!");    
    17     }
    18     else 
    19     {
    20         echo "<font size='7'>".$admin_name.",欢迎您登陆本系统!<br/><br/>";
    21         echo "</font>";
    22     }
    23 
    24     echo "<font size='5'>";
    25     echo "<a href='empList.php'>管理用户</a><br/><br/>";
    26     echo "<a href='addEmp.php'>添加用户</a></a><br/><br/>";
    27     echo "<a href='#'>等待添加</a></a><br/><br/>";
    28     echo "<a href='#'>等待添加</a></a><br/><br/>";
    29     echo "</font size='5'>";
    30     echo "</center>";
    31 ?>
    View Code

    empProcess.php

     1 <?php
     2 /**
     3  * 进行用户验证
     4  */
     5 require_once 'common.php';
     6 checkUser();
     7 
     8 require_once 'EmpService.class.php';
     9 
    10 $type=$_REQUEST['type'];
    11 if($type=="del")
    12 {
    13     $id=$_GET['id'];
    14     $pageNow=$_GET['pageNow'];
    15     if(empty($id))
    16     {
    17         die("没有收到雇员的ID号数据!");
    18     }
    19     //创建一个EmpService的对象
    20     $empservice=new EmpService();
    21     $flag=$empservice->deleteUser($id);
    22     if($flag==1)
    23     {
    24         header("Location:success.php?id={$id}&pageNow={$pageNow}");
    25         exit();
    26     }
    27     header("Location:error.php?id={$id}");
    28     exit();
    29 }
    30 else
    31     if($type=="add")
    32     {
    33         $empService=new EmpService();
    34         $name=$_POST['name'];
    35         $grade=$_POST['grade'];
    36         $email=$_POST['email'];
    37         $salary=$_POST['salary'];
    38         $result=$empService->addEmp($name, $grade, $email, $salary);
    39         if($result!=1)
    40         {
    41             die("添加用户失败!".mysql_errno());
    42         }
    43         else 
    44         {
    45             echo "添加成功!<a href='addEmp.php'>返回上一级</a>";
    46         }
    47     }
    48 else 
    49     if($type=="update")
    50     {
    51         $empService=new EmpService();
    52         $id=$_POST['id'];
    53         $name=$_POST['name'];
    54         $grade=$_POST['grade'];
    55         $email=$_POST['email'];
    56         $salary=$_POST['salary'];
    57         $result=$empService->updateEmp($name, $grade, $email, $salary,$id);
    58         if($result!=1)
    59         {
    60             die("更新用户信息失败!".mysql_errno());
    61         }
    62         else
    63         {
    64             echo "更新用户信息成功!<a href='updateEmpUI.php?id={$id}'>返回上一级</a>";
    65         }
    66     }
    67 
    68 ?>
    View Code

    EmpService.class.php

      1 <?php
      2 require_once 'SqlHelper.class.php';
      3 require_once 'Emp.class.php';
      4 class EmpService
      5 {
      6     private $sqlhelper;
      7     private $res;
      8     public function __construct()
      9     {
     10         $this->sqlhelper=new SqlHelper("localhost","root","5a6f38", "empmanage");
     11     }
     12     /**
     13      * 这个方法专门用于更新雇员信息
     14      */
     15     public function updateEmp($name, $grade, $email, $salary,$id)
     16     {
     17         $sql="update emp set name='{$name}',grade={$grade},
     18         email='{$email}',salary={$salary} where id={$id}";
     19         $result=$this->sqlhelper->dml($sql);
     20         mysql_close($this->sqlhelper->conn);
     21         return $result;
     22     }
     23     /**
     24      * 这个方法通过ID号取得雇员的其他信息
     25      */
     26     public function getEmpById($id)
     27     {
     28         $emp=new Emp();
     29         $sql="select * from emp where id={$id}";
     30         $this->res=$this->sqlhelper->dql($sql);
     31         if(!$this->res)
     32         {
     33             die("查询失败!".mysql_errno());
     34         }
     35         if(($row=mysql_fetch_assoc($this->res))!=null)
     36         {
     37             $emp->setid($row['id']);
     38             $emp->setemail($row['email']);
     39             $emp->setgrade($row['grade']);
     40             $emp->setname($row['name']);
     41             $emp->setsalary($row['salary']);
     42         }
     43         $this->closeAll();
     44         return $emp;
     45     }
     46     /**
     47      * 这个方法是专门针对添加用户的方法
     48      */
     49     public function addEmp($name,$grade,$email,$salary)
     50     {
     51         $sql="insert into emp(name,grade,email,salary) values ('{$name}',{$grade},'{$email}',{$salary})";
     52         //$this->sqlhelper=new SqlHelper("localhost", "root","5a6f38", "empmanage");
     53         $result=$this->sqlhelper->dml($sql);
     54         mysql_close($this->sqlhelper->conn);
     55         return $result;
     56     }
     57     public function getRowCount()
     58     {
     59         $sql="select count(*) from emp";
     60         $res=$this->sqlhelper->dql($sql);
     61         if(!$res)
     62         {
     63             die("查询失败!");
     64         }
     65         $row=mysql_fetch_row($res);
     66         $rowCount=$row[0];
     67         return $rowCount;
     68     }
     69     public function getResource($pageNow,$pageSize)
     70     {
     71         $x=($pageNow-1)*$pageSize;
     72         $sql="select * from emp order by id limit ".$x.",".$pageSize;
     73         $res=mysql_query($sql,$this->sqlhelper->conn);
     74         $this->res=$res;
     75         if(!$res)
     76         {
     77             die("获取资源失败!");
     78         }
     79         $arr=Array();//效仿discuz的写法,这是05版本的改进之处。
     80         $top=-1;
     81         while(($row=mysql_fetch_assoc($res))!=null)
     82         {
     83             $arr[++$top]=$row;
     84         }
     85         return $arr;
     86     }
     87     public function getSqlHelper()
     88     {
     89         return $this->sqlhelper;
     90     }
     91     public function getResource_unArray()
     92     {
     93         return $this->res;
     94     }
     95     public function closeAll()
     96     {
     97         mysql_free_result($this->res);
     98         mysql_close($this->sqlhelper->conn);
     99     }
    100     public function closeConn()
    101     {
    102         mysql_close($this->sqlhelper->conn);
    103     }
    104     public function deleteUser(&$id)
    105     {
    106         $sql="delete from emp where id={$id}";
    107         return $this->sqlhelper->dml($sql);
    108     }
    109 }
    110 ?>
    View Code

    error.php

    1 <?php
    2 echo "删除失败!";
    3 ?>
    View Code

    loginProcess.php

     1 <?php
     2 // print_r($_COOKIE);
     3 require_once 'AdminService.class.php';
     4 $admin_id=$_POST['admin_id'];
     5 $admin_password=$_POST['admin_password'];
     6 $userinf=$_POST['userinf'];//记录用户是否要求浏览器记住用户密码
     7 $checkCode=$_POST['checkCode'];
     8 session_start();
     9 
    10 if($checkCode!=$_SESSION['checkCode'])
    11 {
    12     header("Location:login.php?error=验证码输入错误,请重新输入!");
    13     exit();
    14 }
    15 if(empty($userinf))//用户不允许记住用户名和密码
    16 {
    17 //     echo "用户不允许记住账号密码<br/>";
    18     //查看以前是否有cookie记录,如果有,则删除全部
    19     if(!empty($_COOKIE['name']))
    20     {
    21 //         echo "将会删除cookie<br/>";
    22         setCookie("name",$admin_id,Time()-14*24*3600);
    23         setCookie("password",$admin_password,Time()-14*24*3600);
    24         setCookie("lasttime",date("Y年m月d日 H时m分s秒"),Time()-14*24*3600);
    25     }
    26 }
    27 else //用户允许记住用户名和密码
    28 {
    29 //     echo "用户允许浏览器记住账号密码<br/>";
    30     //先查看cookie中是否有以前的记录,如果有,显示上一次登录的时间;反之
    31     //提示是首次登陆,并保存住账号密码
    32     if(empty($_COOKIE['name']))//
    33     {
    34 //         echo "将会创建cookie<br/>";
    35         setCookie("name",$admin_id,Time()+14*24*3600);
    36         setCookie("password",$admin_password,Time()+14*24*3600);
    37     }
    38 }
    39 // exit();
    40 
    41 $adminservice=new AdminService();
    42 $flag=$adminservice->checkAdmin($admin_id, $admin_password);
    43 if($flag==1)
    44 {
    45     session_start();
    46     /**
    47         将数据保存到session中,进行用户登录校验
    48      */
    49     $_SESSION['name']=$admin_id;
    50     header("Location:empMain.php?name=".$adminservice->getName($admin_id));
    51     exit();
    52 }
    53 else
    54 {
    55     header("Location:login.php?error=用户名或者密码错误,请重新输入!");
    56     exit();
    57 }
    58 ?>
    View Code

    SqlHelper.class.php

     1 <?php
     2 class SqlHelper
     3 {
     4     public $host;
     5     public $user;
     6     public $psw;
     7     public $dbname;
     8     public $conn;
     9     public function __construct($host,$user,$psw,$dbname)
    10     {
    11         $this->host=$host;
    12         $this->user=$user;
    13         $this->psw=$psw;
    14         $this->dbname=$dbname;
    15         $this->conn=mysql_connect($this->host,$this->user,$this->psw);
    16         if(!$this->conn)
    17         {
    18             die("数据库连接失败!".mysql_error());
    19         }
    20         mysql_select_db($this->dbname);
    21         mysql_query("set names utf8",$this->conn);
    22     }
    23     public function dql($sql)
    24     {
    25         $res=mysql_query($sql,$this->conn);
    26         if(!$res)
    27         {
    28             die("查询失败!".mysql_error());
    29         }
    30         else return $res;
    31     }
    32     public function dml($sql)
    33     {
    34         $res=mysql_query($sql,$this->conn);
    35         if(!$res)
    36         {
    37             die("数据更新失败!".mysql_error());
    38         }
    39         else if(mysql_affected_rows($this->conn))
    40         {
    41             return 1;
    42         }
    43         else 
    44         {
    45             return 2;
    46         }
    47     }
    48 }
    49 ?>
    View Code

    success.php

    1 <?php
    2     $pageNow=$_GET['pageNow'];
    3     echo "删除成功!";
    4     echo "<a href='empList.php?pageNow={$pageNow}'>返回上一级</a>";
    5 ?>
    View Code

    updateEmpUI.php

     1 <?php
     2 /**
     3  * 进行用户验证
     4  */
     5 require_once 'common.php';
     6 checkUser(); 
     7 ?>
     8 <html>
     9 <!--这个是专门针对更新用户信息设计的界面-->
    10 <head>
    11 <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    12 </head>
    13 <body>
    14 <?php
    15 require_once 'Emp.class.php';
    16 require_once 'EmpService.class.php';
    17 $id=$_GET['id'];
    18 //获得其他参数。
    19 $empService=new EmpService();
    20 //获取Emp对象。
    21 $emp=$empService->getEmpById($id);
    22 ?>
    23 <center><h1><font size='30px'>这里是更新用户信息的界面</font></h1></center>
    24 <br/>
    25 <br/>
    26 <form action="empProcess.php" method="post">
    27 <table align="center" cellpadding="10px">
    28 <tr align="center">
    29 <td>您的ID是:</td>
    30 <td><input readonly="readonly" type="text" name="id" value="<?php echo $emp->getid()?>"/></td>
    31 </tr>
    32 
    33 <tr align="center">
    34 <td>请输入您的新姓名:</td>
    35 <td><input type="text" name="name" value="<?php echo $emp->getname()?>"/></td>
    36 </tr>
    37 
    38 <tr align="center">
    39 <td>请输入您的新等级:</td>
    40 <td><input type="text" name="grade" value="<?php echo $emp->getgrade()?>"/></td>
    41 </tr>
    42 
    43 <tr align="center">
    44 <td>请输入您的新邮箱:</td>
    45 <td><input type="text" name="email" value="<?php echo $emp->getemail()?>"/></td>
    46 </tr>
    47 
    48 <tr align="center">
    49 <td>请输入您的新薪水:</td>
    50 <td><input type="text" name="salary" value="<?php echo $emp->getsalary()?>"/></td>
    51 </tr>
    52 <input type="hidden" name="type" value="update"/>
    53 <tr align="center">
    54 <td><input type="submit" name="submit" value="单击提交" /></td>
    55 <td><input type="reset" name="reset" value="重新输入" /></td>
    56 </tr>
    57 <tr>
    58 <td colspan="2"><a href="empMain.php?name=admin">单击返回上一级</a></td>
    59 </tr>
    60 </table>
    61 </form>
    62 </body>
    63 </html>
    View Code

    这个小项目还只是个半成品。仅仅完成了对雇员的增删改功能,查找功能还未实现。但是实现了cookie、session、验证码的小功能。特别是小项目是采用的mvc架构,这就大大增加了开发难度但是代码复用性大大提高了。

    项目截图:

  • 相关阅读:
    java.sql.SQLException: The server time zone value is unrecognized or represents more than one time zone
    MySQL
    C# 简单软件有效期注册的实现【原】
    【转】Flask and PostgreSQL on Heroku
    5个提问,详细介绍北极星指标的策略框架 | 人人都是产品经理
    DingDing CRM
    Mariadb & MySQL :: MySQL 5.7 Reference Manual :: 8.2.1.17 LIMIT Query Optimization
    IT从之“CRM”与“OA”
    maven
    中国网络安全企业100强报告
  • 原文地址:https://www.cnblogs.com/kuangdaoyizhimei/p/3970255.html
Copyright © 2011-2022 走看看