我想创建一个表单,允许用户键入不同字段的搜索,并让它从数据库中选择正确的值并显示它们。出于某些原因,我无法弄清楚如何使用Mysqli查询定义搜索页面。
如何根据搜索表单和mysqli查询创建一个显示表中记录的search.php
页面?
这是我迄今为止所做的尝试:
PHP代码:
$DBhost = "localhost";
$DBuser = "root";
$DBpass = "";
$DBname = "search";
$DBcon = new MySQLi($DBhost,$DBuser,$DBpass,$DBname);
if ($DBcon->connect_errno) {
die("ERROR : -> ".$DBcon->connect_error);
}
$output = '';
if(isset($_POST['search'])) {
$query = mysqli_query($DBcon,"SELECT * FROM data WHERE name LIKE '%" . $_POST['name'] . "%'
OR address LIKE '%" . $_POST['address'] . "%'
OR city LIKE '%" . $_POST['city'] . "%'
OR state LIKE '%" . $_POST['state'] . "%'") or die ("Could not search");
}
HTML代码:
<!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>MySQL table search</title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<style>
BODY, TD {
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
}
</style>
</head>
<body>
<form action ="index.php" method = "post">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address" /></td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" name="city" /></td>
</tr>
<tr>
<td>State:</td>
<td><input type="text" name="state" /></td>
</tr>
<tr>
<td></td>
<td><input name="search" type="submit" value="Search"/></td>
</tr>
</table>
</form>
<?php echo ("$output");?>
</body>
</html>
答案 0 :(得分:0)
根据我对你的问题的理解,你喜欢在sql中搜索表并回显输出。所以我在这里使用mysqli和sql LIKE运算符来按用户搜索模式。
注意:在我使用LIKE tblname %$vars%
的sql查询中,您也可以使用这种方式%$vars%
但两者都有不同的含义。要了解更多信息,请访问此LINK
<?php
$link = new mysqli ('localhost','root','admin','demo');
if($link->connect_error){
die("failed to connect".$link->connect_error);
}
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action ="" method = "post">
<table>
<tr>
<td>Name:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="address" /></td>
</tr>
<tr>
<td>City:</td>
<td><input type="text" name="city" /></td>
</tr>
<tr>
<td>State:</td>
<td><input type="text" name="state" /></td>
</tr>
<tr>
<td></td>
<td><input name="search" type="submit" value="Search"/></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['search'])){
$output = '';
$name = $_POST['name'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$sql = "SELECT name,address,city,state FROM sql_like_operator WHERE name LIKE '%$name%' OR address LIKE '%$address%' OR city LIKE '%$city%' OR state LIKE '%$state%'";
$res= $link->query($sql);
if($res->num_rows > 0){
while($row = $res->fetch_assoc()){
$output .= $row['name']."<br>";
$output .= $row['address']."<br>";
$output .= $row['city']."<br>";
$output .= $row['state']."<br>";
}
}
else
{
echo "0 results";
}
}
?>
<h5><?php echo @$output;?></h5>
</body>
</html>