65. 雇员管理系统(2)

该博客讲述了雇员管理系统的实现,包括在登录成功后显示用户名的功能,并详细介绍了如何在系统中应用分页技术,提升用户体验。
摘要由CSDN通过智能技术生成

1. 在登陆成功页面显示用户名:

login.php

<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>

<body>
<h1>管理员登陆系统</h1>
<form action="loginProcess.php" method="post">
    <table>
        <tr>
            <td>用户id</td><td><input type="text" name="id"/></td>
        </tr>
        <tr>
            <td>密码</td><td><input type="password" name="password"/></td>
        </tr>
        <tr>
            <td><input type="submit" value="用户登陆"/></td>
            <td><input type="reset" value="重新填写"/></td>
        </tr>
    </table>
</form>
<?php

if(!empty($_GET['errno'])){
    //接收errno
    $errno = $_GET['errno'];
    if($errno == 1){
        echo "<br/><font color='red'>你的用户名或者密码错误</font>";
    }
}
?>
</body>
</html>
loginProcess.php

<?php

$id = $_POST['id'];

$password = $_POST['password'];

//到数据库去验证

//1.得到连接
$link = mysql_connect('localhost','root','123456');
if(!$link){
    die('连接失败' . mysql_error());
}

//2.设置访问数据库的编码形式
mysql_query('set names utf8',$link) or die('set names error' . mysql_error());

//3.选择数据库
mysql_select_db('test',$link) or die(mysql_error());

//4.发送sql语句,验证
//防止sql注入攻击
//改变验证逻辑
//$sql = "select * from admin where id='{$id}' and password='{$password}'";
$sql = "select password,name from admin where id='{$id}' and password='{$password}'";
//1.通过输入的id来获取数据库中的密码,再和输入的密码进行比对
$res = mysql_query($sql,$link);

if($row  = mysql_fetch_assoc($res)){
    //查询到了
    //2.取出数据库的密码
    if($row['password'] == $password){
        //说明合法
        //取出用户的名字
        $name = $row['name'];
        header("Location:empManage.php?name=$name");
        exit();
    }

}
    header('Location:login.php?errno=1');
    exit();

//关闭资源
mysql_free_result($res);
mysql_close($link);



/*if($id == '100'  && $password =='123'){
    //合法,跳转到empManage.php
    header('Location:empManage.php');
    //跳转后要退出,最好退出,退出进程,一个请求对应一个进程
    exit();
}else{
    //非法,跳转回去
    header('Location:login.php?errno=1');//带点信息给login.php
    exit();
}*/
empManage.php

<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>

<body>

<?php
    //显示出用户名
    echo "欢迎 --- " . $_GET['name'] ."--- 登陆成功";
?>
<h1>主页面</h1>
<a href="empList.php">管理用户</a><br/>
<a href="">添加用户</a><br/>
<a href="">查询用户</a><br/>
<a href="">退出系统</a><br/>
</body>
</html>



<?php
    header('Content-Type:text/html;charset=utf-8');
    echo "登陆成功<br/>";
    echo "<br/><a href='login.php'>返回重新登陆</a>";
?>
empList.php

<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>

<body>
<?php
    //显示所有用户的信息(表格)
    //查询数据库
    $mysqli = new MySQLi('localhost','root','123456','test');
    if($mysqli->connect_error){
        die('connect error' . $mysqli->connect_error);
    }
    $mysqli->set_charset('utf8') or die($mysqli->error);
    $sql = "select * from emp;";
    $res = $mysqli->query($sql);
    if(!$res){
        die($mysqli->error);
    }
    while($row = $res->fetch_row()){
        $rows[] = $row;
    }

    $res->free();
    $mysqli->close();
?>
<h1>雇员信息列表</h1>
<table border="1" cellpadding="0" cellspacing="0" width="80%">
    <tr>
        <th>id</th>
        <th>name</th>
        <th>grade</th>
        <th>email</th>
        <th>salary</th>
        <th>操作</th>
    </tr>
    <?php foreach($rows as $row):?>
        <tr>
            <td><?php echo $row[0];?></td>
            <td><?php echo $row[1];?></td>
            <td><?php echo $row[2];?></td>
            <td><?php echo $row[3];?></td>
            <td><?php echo $row[4];?></td>
            <td><a href="">删除用户</a>|<a href="">修改用户</a></td>
        </tr>
    <?php endforeach;?>
</table>
</body>
</html>

2.分页技术

在分页中有几个变量是必须的:
$pageNow :显示第几页,用户输入
$pageCount:共有几页($rowCount/$pageSize 计算出来)
$rowCount:共有多少记录(这个从数据库中获取)
$pageSize:每页显示几条记录(程序员指定)

例子:
$pageNow = 1;
$rowCount = 1;
$pageSize = 1
$pageCount = ceil($rowCount/$pageSize);

这里写图片描述

<html>
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>

<body>
<?php
    //显示所有用户的信息(表格)
    //查询数据库
    $mysqli = new MySQLi('localhost','root','123456','test');
    if($mysqli->connect_error){
        die('connect error' . $mysqli->connect_error);
    }
    $mysqli->set_charset('utf8') or die($mysqli->error);

    //分页显示
    $pageSize = 2;//默认每页显示几行记录
    $rowCount = 0;// 这个变量要从数据库取
    $pageNow = 1;//显示第几页,这是一个变化量(用户指定)

//这里我们需要根据用户的点击来修改 $pageNow
//这里我们需要判断是否有这个pageNow法师,有就使用,没有则使用默认值--显示第一页

if(!empty($_GET['pageNow'])){
    $pageNow = $_GET['pageNow'];
}

    $pageCount = 0;//表示一共有几页,是计算出来的

    $sql = "select count(id) from emp;";
    $res1 = $mysqli->query($sql);
    //取出行数
    if($row = $res1->fetch_row()){
        $rowCount = $row[0];
    }

    //计算共有多少页
    $pageCount = ceil($rowCount/$pageSize);
    $sql = "select * from emp limit " . ($pageNow - 1)*$pageSize . ",$pageSize";

    //var_dump($sql);die();


    //$sql = "select * from emp limit 0,2;";
    $res2 = $mysqli->query($sql);
    if(!$res2){
        die($mysqli->error);
    }
    while($row = $res2->fetch_row()){
        $rows[] = $row;
    }

    $res2->free();
    $mysqli->close();
?>
<h1>雇员信息列表</h1>
<table border="1" cellpadding="0" cellspacing="0" width="80%">
    <tr>
        <th>id</th>
        <th>name</th>
        <th>grade</th>
        <th>email</th>
        <th>salary</th>
        <th>操作</th>
    </tr>
    <?php foreach($rows as $row):?>
        <tr>
            <td><?php echo $row[0];?></td>
            <td><?php echo $row[1];?></td>
            <td><?php echo $row[2];?></td>
            <td><?php echo $row[3];?></td>
            <td><?php echo $row[4];?></td>
            <td><a href="">删除用户</a>|<a href="">修改用户</a></td>
        </tr>
    <?php endforeach;?>
</table>

<?php
    //打印出页码的超链接
    for($i=1;$i<=$pageCount;$i++){
        echo "<a href='empList.php?pageNow=$i'>$i</a>&nbsp;&nbsp;";
    }
?>
</body>
</html>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值