做一个简单的查询,通过搜索关键字来找到自己想要的
<body>
<h1>查询数据</h1>
<form action="chaxun.php" method="post">
<div>
请输入汽车名称:<input type="text" name="name" />
请输入系列代号:<input type="text" name="brand" />
<input type="submit" value="查询" />
</div>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
<tr>
<td>代号</td>
<td>名称</td>
<td>系列</td>
<td>油耗</td>
<td>价格</td>
</tr>
<?php
$tj = " 1=1 ";//做一个恒成立的条件
$name="";
if(!empty($_POST)){
$name = $_POST["name"];
$tj = " name like ‘%{$name}%‘ ";
}
$db = new MySQLi("localhost","root","123","ceshi");
$sql = "select * from car where {$tj}";
$result = $db->query($sql);
if($result){
$arr = $result->fetch_all();
foreach($arr as $v){
echo "<tr>
<td>{$v[0]}</td>
<td>{$v[1]}</td>
<td>{$v[2]}</td>
<td>{$v[4]}</td>
<td>{$v[7]}</td>
</tr>";}
}
?>
</table>
</div>
</body>