使用PHP搜索表

时间:2014-04-01 22:25:19

标签: php mysql

我在我的网站上有一个用于向MySQL表发送信息的表单,然后我有一个名为read.php的PHP文件,它从MySQL表中检索信息并将其显示在页面上。这完全正常,我没有问题。但是我现在想添加一个搜索功能,以便可以通过名字,姓氏,电子邮件等搜索表格。

这是我的read.php文件:

<html>
<head>
<title>messages</title>
</head>
<body>
<h1>Message Center</h1>

<?php

$con = mysqli_connect('localhost', 'removed', 'removed', 'removed');
$result = mysqli_query($con,"SELECT*FROM specialisttable");

echo"<table border='1'>
Specialist Table
<tr>
<th>First Name</th>
<th>Last Name</th>
<th>Phone</th>
<th>Email</th>
<th>Website</th>
<th>Speciality</th>
<th>Street Name</th>
<th>Suburb</th>
<th>City</th>
<th>Post Code</th>
<th>Comments</th>
</tr>";

while($row = mysqli_fetch_array($result))
{

{
  echo "<tr>";
  echo "<td>" . $row['firstname'] . "</td>";
  echo "<td>" . $row['lastname'] . "</td>";
  echo "<td>" . $row['phone'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "<td>" . $row['website'] . "</td>";
  echo "<td>" . $row['speciality'] . "</td>";
  echo "<td>" . $row['streetname'] . "</td>";
  echo "<td>" . $row['suburb'] . "</td>";
  echo "<td>" . $row['city'] . "</td>";
  echo "<td>" . $row['postcode'] . "</td>";
  echo "<td>" . $row['comments'] . "</td>";
  echo "</tr>";
}

}
echo"</table>";
mysqli_close($con);

?>
<br>
<br>
<a href="index.html">Click to return to homepage</a>
</body>
</html>

我希望通过名字,电子邮件和城市等搜索来搜索,例如,如果我有一个名为John的条目,我在第一个名称搜索栏中键入J,他的其他信息将显示在表格中以及其他名字中有J的人。 对于诸如城市之类的东西也是同样的事情,如果我输入auck,那么城市部分中的所有条目都将显示以及其他信息。

我不确定如何执行此操作,我已设法找出如何使用$search = $_GET['search'];搜索名字,然后使用:

while($row = mysqli_fetch_array($result))
{
if($search=="")
{
  echo "<tr>";
  echo "<td>" . $row['firstname'] . "</td>";
  echo "<td>" . $row['lastname'] . "</td>";
  echo "<td>" . $row['phone'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "<td>" . $row['website'] . "</td>";
  echo "<td>" . $row['speciality'] . "</td>";
  echo "<td>" . $row['streetname'] . "</td>";
  echo "<td>" . $row['suburb'] . "</td>";
  echo "<td>" . $row['city'] . "</td>";
  echo "<td>" . $row['postcode'] . "</td>";
  echo "<td>" . $row['comments'] . "</td>";
  echo "</tr>";
}
else if($row['firstname']==$search)
{
echo "<tr>";
  echo "<td>" . $row['firstname'] . "</td>";
  echo "<td>" . $row['lastname'] . "</td>";
  echo "<td>" . $row['phone'] . "</td>";
  echo "<td>" . $row['email'] . "</td>";
  echo "<td>" . $row['website'] . "</td>";
  echo "<td>" . $row['speciality'] . "</td>";
  echo "<td>" . $row['streetname'] . "</td>";
  echo "<td>" . $row['suburb'] . "</td>";
  echo "<td>" . $row['city'] . "</td>";
  echo "<td>" . $row['postcode'] . "</td>";
  echo "<td>" . $row['comments'] . "</td>";
  echo "</tr>";
}

我可以为姓氏和任何其他字段做同样的事情,但它使代码非常混乱,我不认为这是最有效的做事方式。

如果有人可以请我指出如何有效地制作搜索功能,我会非常感激。

非常感谢。

0 个答案:

没有答案