PHP显示员工信息

这是一个使用PHP实现的员工信息列表展示页面,通过连接MySQL数据库获取数据。页面包含员工的ID、姓名、所属部门、出生日期和入职时间等信息,并提供编辑和删除操作。当数据库中无数据时,显示提示信息。代码中使用了`mysql_connect`进行数据库连接,`mysql_query`执行SQL查询,`mysql_fetch_assoc`遍历结果集。页面样式采用内联样式表定义,结构清晰,易于理解。
摘要由CSDN通过智能技术生成
<?php if (!defined('APP')) die('error!');?>
<!doctype html>
<html>
 <head>
<meta charset= "utf-8">
<title>员工信息列表</title>
<style>
.box {margin: 20px; }
.box.title{ font-size: 22px; font-weight: bold; text-align: center; }
.box table {width: 100%;margin-top: 15px; border-collapse: collapse; font-size: 12px;
border: 1px solid #B5D6E6;min-width: 460px; }
.box table th,.box table td{height: 20px;border: 1px solid #B5D6E6; }
.box table th{background-color: #E8F6FC; font-weight: normal; }
.box table td{text-align: center; }
</style>
</head>
<body>
<div class= "box">
<div class="title">员 工信息列表</div>
<table border="1">
<tr>
<th width= "5%">ID</th><th>姓名</th><th>所属部门</th><th>出生日期</th><th>入职时间</th>
<th width= "25%">相关操作</th>
</tr>
<?php if(!empty ($emp_info)) { ?>
<?php foreach ($emp_info as $row) { ?>
<tr>
<td><?php echo $row['e_id']; ?></td>
<td><?php echo $row ['name'] ; ?></td>
<td><?php echo $row['e_dept'] ; ?></td>
<td><?php echo $row[ 'date of birth'] ; ?></td>
<td><?php echo $row[ 'date of entry' ] ; ?></td>

<td><div align= "center"><span><img src= "images/edt.gif" width="16" height="16" />
编辑&nbsp; &nbsp; <img src= "images/del.gif" width="16" height="16" />删除</span></div>
</td>
</tr>
<?php } ?>
<?php }else {
?>
<tr><td colspan="6">暂无员工数据! </td></tr>
<?php } ?>
</table>
</div>
</body>
</html>

<?php
//声明文件解析的编码格式
header('content-type:text/html;charset=utf-8');
//连接数据库
$link = mysql_connect('localhost','root','123');
//判断数据库连接是否成功,如果不成功则显示错误信息并终止脚本继续执行
if(!$link){
	die('连接数据库失败!'.mysql_error());
}
//设置字符集,选择数据库
mysql_query('set names utf8');
mysql_query('use itcast');

//准备SQL语句
$sql = 'select * from  emp_info';

//执行SQL语句,获取结果集
$res = mysql_query($sql,$link);
if(!$res) 
    die(mysql_error());
    //定义员工数组,用以保存员工信息
    $emp_info = array();
    
    //遍历结果集,获取每位员工的详细数据
    while($row = mysql_fetch_assoc($res)){
        //把从结果集中取出的每一行数据赋值给$emp_info数组
        $emp_info[] = $row;
    }
    //设置常量,用以判断视图页面是否由此页面加载
    define('APP', 'itcast');
    
    //加载视图页面,显示数据
    require './list_html.php';
    ?>

效果图

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Zaltana

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值