<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<hl>表单的关键字查询</h1>
<form action="Untitled-1.php" method="post">
<div>
请输入查询的名字:<input type="text" name="name" />
<input type="submit" value="查询" />
</div>
</form>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>ID</td>
<td>名字</td>
<td>性别</td>
<td>生日</td>
<td>地址</td>
</tr>
<?php
//实现两个逻辑
//1.如果没有POST数据,查询所有
//2.如果有POST数据,根据条件查询
$db = new MySQLi("localhost","root","密码","数据库名字");
$tj = " 1 = 1 "; //默认条件
if(!empty($_POST))
{
$name = $_POST['name'];
$tj = " name like '%{$name}%' ";
}
//将条件拼接到SQL语句
$sql = "select * from 表名 where {$tj}";
$result = $db -> query($sql);
while($arr = $result -> fetch_row())
{
//关键字的特殊显示
$name; $arr[1];
$str = str_replace($name,"<mark>{$name}</mark>",$arr[1]);
echo "<tr>
<td>{$arr[0]}</td>
<td>{$str}</td>
<td>{$arr[2]}</td>
<td>{$arr[3]}</td>
<td>{$arr[5]}</td>
</tr>";
}
?>
</table>
</body>
</html>
效果图:
查询张