码迷,mamicode.com
首页 > Web开发 > 详细

php多关键字查询

时间:2016-12-23 09:37:18      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:localhost   oct   nbsp   ima   idt   for   src   charset   att   

  php单一关键字查询

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 tdansitional//EN" "http://www.w3.org/td/xhtml1/DTD/xhtml1-tdansitional.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>
<h1>查询页面</h1>

<?php
$db = new MySQLi("localhost","root","","12345");

$tj = " 1=1 "; //条件默认恒成立

$name = "";

if(!empty($_POST["name"]))
{
	$name = $_POST["name"];
	$tj = " name like ‘%{$name}%‘ ";
}

?>

<form action="test.php" method="post">
<div>请输入名称:
<input type="text" name="name" /> 
<input type="submit" value="查询" />
</div>
</form>
<br>

<table width="100%" border="1" cellpadding="0" cellspacing="0">
	<tr>
        <td>代号</td>
        <td>名称</td>
        <td>系列</td>
        <td>时间</td>
        <td>油耗</td>
        <td>功率</td>
    </tr>
    <?php

	$sql = "select * from car where {$tj}";
	echo $sql;
	
	$result = $db->query($sql);
	$attr = $result->fetch_all();
	
	foreach($attr as $v)
	{
		$str = str_replace($name,"<mark>{$name}</mark>",$v[1]);
		echo "<tr>
        <td>{$v[0]}</td>
        <td>{$str}</td>
        <td>{$v[2]}</td>
        <td>{$v[3]}</td>
        <td>{$v[4]}</td>
        <td>{$v[5]}</td>
    </tr>";
	}
	
	?>
</table>
</body>
</html>

 

 

结果:

 

技术分享

关键字查找“325”:

技术分享

 

 

多条件查询

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 tdansitional//EN" "http://www.w3.org/td/xhtml1/DTD/xhtml1-tdansitional.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>
<h1>查询页面</h1>

<?php
$db = new MySQLi("localhost","root","","12345");

$tj1 = " 1=1 ";
$tj2 = " 1=1 ";
$name = "";

if(!empty($_POST["name"]))
{
	$name = $_POST["name"];
	$tj1 = " name like ‘%{$name}%‘ ";
}

if(!empty($_POST["brand"]))
{
	$brand = $_POST["brand"];
	$tj2 = " brand = ‘{$brand}‘ ";
}

?>

<form action="main.php" method="post">
<div>请输入名称:
<input type="text" name="name" value="<?php echo $name ?>" /> 

请输入系列代号:
<input type="text" name="brand" />

<input type="submit" value="查询" />
</div>
</form>
<br>

<table width="100%" border="1" cellpadding="0" cellspacing="0">
	<tr>
        <td>代号</td>
        <td>名称</td>
        <td>系列</td>
        <td>时间</td>
        <td>油耗</td>
        <td>功率</td>
    </tr>
    <?php

	$sql = "select * from car where {$tj1} and {$tj2}";
	echo $sql;
	
	$result = $db->query($sql);
	$attr = $result->fetch_all();
	
	if(count($attr)>0)
	{
		foreach($attr as $v)
		{
			$str = str_replace($name,"<mark>{$name}</mark>",$v[1]);
			echo "<tr>
			<td>{$v[0]}</td>
			<td>{$str}</td>
			<td>{$v[2]}</td>
			<td>{$v[3]}</td>
			<td>{$v[4]}</td>
			<td>{$v[5]}</td>
		</tr>";
		}
	}
	else
	{
		echo "<script>alert(‘没有查到数据‘);</script>";
	}
	
	
	
	?>
</table>
</body>
</html>

  

 

结果:

 

技术分享

查询关键字“奥迪”,代号为“b002”的结果:

技术分享

 

php多关键字查询

标签:localhost   oct   nbsp   ima   idt   for   src   charset   att   

原文地址:http://www.cnblogs.com/cyrfr/p/6213512.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!