要求
实现全字段搜索
效果图
index.php代码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<style>
body {
background-color: #dedede;
}
.main {
width: 1058px;
margin: 50px auto;
}
h1 {
margin: 50px auto;
text-align: center;
}
form {
margin-bottom: 20px;
}
a {
text-decoration: none;
color: #000;
}
a:hover {
color: deeppink;
}
.edit {
color: #0e84b5;
}
.del {
color: #9A0000;
}
table {
border-collapse: collapse;
margin: 0 auto;
}
th {
background-color: #00bcff;
padding: 20px 40px;
}
tr {
text-align: center;
}
td {
padding: 20px 40px;
}
</style>
</head>
<body>
<?php
//连接数据库
$link = mysqli_connect("localhost", "root", "root");
if (!$link) {
echo die("数据库链接失败") . mysqli_error();
} else {
//选择数据库
mysqli_query($link, "use zhuang");
/* ——————排序—————— */
// 初始化排序语句,用来组合排序的order子句
$sql_order = "";
//允许排序的字段
$field = array('e_dept', 'date_of_entry');
//判断$_GET['order']是否存在,如果存在则将其赋值给$order
if (isset($_GET['order'])) {
$order = $_GET['order'];
} else {
// 如果不存在则把空字符串赋值给$order
$order = '';
}
if (isset($_GET['sort'])) {
$sort = $_GET['sort'];
} else {
$sort = '';
}
/* ——————搜索—————— */
//输入数值初始化为空
$sql_where = "";
//判断是否存在输入的数值
if (isset($_GET['txt'])) {
//变量等于输入数值
$where = $_GET['txt'];
//用户输入的搜索信息不能直接使用,可能存在导致SQL语句执行失败,需要使用函数进行转义
$where = mysqli_real_escape_string($link, $where);
//数据库模糊搜索输入数值
$sql_where = "where `e_id` like '%$where%' or `e_name` like '%$where%' or `e_dept` like '%$where%' or `date_of_birth` like '%$where%' or `date_of_entry` like '%$where%'";
}
//判断$order是否存在于合法字段列表$fields中
if (in_array($order, $field)) {
$sql_where = $_GET['where'];
//判断$_GET['sort']是否存在并且值是否为'desc'
if ($sort == 'desc') {
//条件成立,组合order子句 order by 字段 desc
$sql_order = "order by $order desc";
//更新$sort为'asc'
$sort = 'asc';
} else {
//条件不成立,组合order子句 order by 字段 asc
$sql_order = "order by $order asc";
//更新$sort为'desc'
$sort = 'desc';
}
}
//选择表
$sql = "select * from emp_info $sql_where $sql_order";
$res = mysqli_query($link, $sql);
//定义员工数组,用以保存员工信息
$arr = array();
//遍历结果集,获取每位员工的详细数据
while ($row = mysqli_fetch_row($res)) {
$arr[] = $row;
}
}
?>
<div class="main">
<h1>展示员工信息</h1>
<form action="index.php" method="get">
<input type="text" name="txt">
<input type="submit" value="搜索">
</form>
<table border="1" style="border-collapse: collapse;">
<tr>
<th>ID</th>
<th>姓名</th>
<th><a href="index.php?order=e_dept&sort=<?php echo ($order == 'e_dept') ? $sort : 'desc'; ?>&where=<?php echo isset($sql_where) ? $sql_where : ' '; ?>">所属部门</a></th>
<th>出生日期</th>
<th><a href="index.php?order=date_of_entry&sort=<?php echo ($order == 'date_of_entry') ? $sort : 'desc'; ?>&where=<?php echo isset($sql_where) ? $sql_where : ' '; ?>">入职日期</a></th>
<th>相关操作</th>
</tr>
<?php foreach ($arr as $v) : ?>
<tr>
<?php foreach ($v as $a) : ?>
<td><?php echo $a; ?> </td>
<?php endforeach; ?>
<td>
<a href="#" onclick="alert('Error:编辑失败');"><span class="edit">✎</span>编辑</a>
<a href="#" onclick="alert('Error:删除失败');"><span class="del">✘</span>删除</a>
</td>
</tr>
<?php endforeach; ?>
</table>
</div>
</body>
</html>
data-z.sql代码
create database `zhuang`;
use `zhuang`;
CREATE TABLE `emp_info` (
`e_id` int unsigned PRIMARY KEY AUTO_INCREMENT,
`e_name` varchar(20) NOT NULL,
`e_dept` varchar(20) NOT NULL,
`date_of_birth` timestamp NOT NULL,
`date_of_entry` timestamp NOT NULL
)CHARSET=utf8;
insert into `emp_info` values
(1, '张三', '市场部', '2008-4-3 13:33:00', '2014-9-22 17:53:00'),
(2, '李四', '开发部', '2008-4-3 13:33:00', '2013-10-24 17:53:00'),
(3, '王五', '媒体部', '2008-4-3 13:33:00', '2015-4-21 13:33:00'),
(4, '赵六', '销售部', '2008-4-3 13:33:00', '2015-3-20 17:54:00'),
(5, '小兰', '人事部', '1989-5-4 17:33:00', '2015-4-1 17:35:00'),
(6, '小新', '媒体部', '1993-9-18 17:36:00', '2015-2-28 17:36:00'),
(7, '小白', '市场部', '1991-10-17 17:37:00', '2014-8-16 17:37:00'),
(8, '小智', '运维部', '1987-6-20 17:37:00', '2015-1-10 17:38:00');
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/83737.html