<?php
//case 13 员工信息搜索
/*
*建立数据表的sql语句
*
* create table emp_info(
emp_id int unsigned auto_increment primary key,
emp_name varchar(20) not null,
emp_dept varchar(20) not null,
date_birth timestamp not null,
date_entry timestamp not null
)charset = utf8;
insert into emp_info values(1,'张三','推广部','1980-05-01','2020-01-10');
insert into emp_info values(2,'李四','人资部','1980-05-01','2020-01-10');
insert into emp_info values(3,'王五','公关部','1980-05-01','2020-01-10');
insert into emp_info values(4,'麻六','销售部','1980-05-01','2020-01-10');
insert into emp_info values(5,'小兰','人事部','1989-4-5','2015-1-4');
insert into emp_info values(6,'小新','广告部','1993-8-19','2016-2-24');
insert into emp_info values(7,'小白','财务部','1991-10-5','2017-8-14');
insert into emp_info values(8,'小智','运维部','1978-11-25','2000-3-19');
*
*/
header('Content-type:text/html;charset=utf8');
$connect = mysqli_connect('localhost','root','mysql123','test');
if(!$connect)
{
die('数据库连接失败!'.mysqli_error());
}
//mysql中字符集是utf8,不是uft-8
mysqli_query($connect,"set names utf8");
$where = '';
//是否输入查询关键字
if(isset($_GET['keyword']))
{
$keyword = $_GET['keyword'];
//防止sql注入
$keyword = mysqli_real_escape_string($connect,$keyword);
$where = "where emp_name like '%{$keyword}%'";
}
//拼接字符串
$sql = "select * from emp_info ".$where;
$result = mysqli_query($connect,$sql);
$emp_info = array();
while($row = mysqli_fetch_assoc($result))
{
$emp_info[] = $row;
}
?>
<html>
<head>
<meta charset="UTF-8">
<title>员工信息列表</title>
</head>
<body>
<div>员工信息列表</div>
<form method="get" action="">
<div style="margin: 10px 0;">
快速查询:<input type="text" name="keyword" id="keyword" style="width: 400px;height30px;">
<input type="submit" name="submit" id="submit" value="查询" style="width: 150px;height:24px;">
</div>
<table border="1" cellspacing="0" cellpadding="0" width="50%" style="text-align: center;">
<tr>
<th>ID</th>
<th>姓名</th>
<th>部门</th>
<th>出生日期</th>
<th>入职时间</th>
<th>相关操作</th>
</tr>
<?php
if(!empty($emp_info))
{
foreach ($emp_info as $row1)
{
echo "<tr>";
echo "<td>{$row1['emp_id']}</td>";
echo "<td>{$row1['emp_name']}</td>";
echo "<td>{$row1['emp_dept']}</td>";
echo "<td>{$row1['date_birth']}</td>";
echo "<td>{$row1['date_entry']}</td>";
echo "<td><span>编辑 删除</span></td>";
echo "</tr>";
}
}
else
{
echo "<tr><td colspan='6'>暂无员工数据!</td></tr>";
}
mysqli_free_result($result);
mysqli_close($connect);
?>
</table>
</form>
</body>
</html>