PHP/MySQL 搜索...默认显示所有数据,或显示匹配的数据

标签 php mysql search

我已经构建了一个显示整个 MySQL 表的页面,但是我现在想在其中添加一个搜索。

我所做的是将其设置为当用户搜索时显示匹配的结果,如果没有搜索则显示整个表格。它显示了表格,但是当我搜索时它没有返回任何数据(尽管其他一切正常)

这就是我要搜索的...

<?php
if(isset($_POST['submit'])){
if(isset($_GET['query'])){
if(preg_match("/^[  a-zA-Z]+/", $_POST['query'])){
$query=$_GET['query'];
$q=mysql_query("SELECT * FROM employees WHERE name LIKE '%$query%'" ) or die("could not search");
$result = mysql_query($q) or die(mysql_error());
while ($row = mysql_fetch_array($result)){

我不知道如何格式化它。我的搜索框是“查询”。任何帮助将不胜感激!

----- 编辑 ----- 我无法让它继续工作...有任何指示吗?

<?php if(!empty($_SESSION['LoggedIn']) && !empty($_SESSION['Username']){?><br /><form method="post" action="search.php">
<input name="query" type="text" required class="forms" id="query" placeholder="Search name..." size="35" />
<input type="submit" name="submit" id="submit" value=" Search " HEIGHT="25" WIDTH="70" BORDER="0" ALT="Submit"><button onclick="window.location.href='search.php'"> Clear </button></form><br />
<table border="0" cellspacing="2" class="data"><tr>
<td align="center" class="idtd"><strong>ID</strong></td>
<td align="center" class="nametd"><strong>Name</strong></td>
<td align="center" class="positiontd"><strong>Position</strong></td>
<td align="center" class="banktd"><strong>Bank</strong></td>
<td align="center" class="pooltd"><strong>Pool</strong></td>
<td align="center" class="starttd"><strong>Start Date</strong></td>
<td align="center" class="endtd"><strong>End Date</strong></td>
<td align="center" class="ghourstd"><strong>Gross Hours</strong></td>
<td align="center" class="chourstd"><strong>Cont'd Hours</strong></td>
</tr></table>
<?php
if(isset($_POST['submit'])){$where = !empty($_GET['query']) ? $db->real_escape_string($_GET['query']) : "";
$q = mysql_query("SELECT * FROM employees WHERE name LIKE '% " . $where . "%'") or die(mysql_error());
while ($row = mysql_fetch_array($result)){
echo "<table class='data' border='0' cellspacing='2'><tr>
<td align='center' class='idtd'>".$row['id']."</td>
<td align='center' class='nametd'>".$row['name']."</td>
<td align='center' class='positiontd'>".$row['position']."</td>
<td align='center' class='banktd'>".$row['bank']."</td>
<td align='center' class='pooltd'>".$row['pool']."</td>
<td align='center' class='starttd'>".$nStartDate."</td>
<td align='center' class='endtd'>".$row['enddate']."</td>
<td align='center' class='ghourstd'>".$row['grosshours']."</td>
<td align='center' class='chourstd'>".$row['contractedhours']."</td><tr ></table>";}}}} else{ ?>
<h1>You must be logged in to view this page.</h1><?php } ?>

最佳答案

借助下面的一些变量

$condition = '1 = 1';//which will result all rows (1=1 is TRUE)
if($_GET['query'])
{
  $query=$_GET['query'];
  $condition = " name LIKE '%$query%'";// this will search only with querydata
}

$q=mysql_query("SELECT * FROM employees WHERE '$condition'" ) or die("could not search");

$result = mysql_query($q) or die(mysql_error());//you have double mysql_query() remove this

最重要的是停止使用很久以前就弃用的 mysql_* 函数,使用 mysqli_* 或 pdo,不要认为这些是新的和困难的,一旦看过教程 example .

关于PHP/MySQL 搜索...默认显示所有数据,或显示匹配的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30761484/

相关文章:

jquery - 如何始终在我的搜索框中显示加号图标

search - 统一成本搜索和最佳优先搜索方法有什么区别?

php - 通过代理服务器访问 Import.io API

php - 在 SQL 中反序列化 PHP 数组

php - Yii CDBCriteria 复杂连接查询

java - SQL Where 子句不返回任何内容 - 初学者

php - Joomla 组件包更新程序

php - 如何通过 PHP 中的 SAFE MODE 限制?

mysql 过程没有返回正确的值

python - 如何在 MongoDB/Python/Django 中按名字和姓氏搜索用户