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

时间:2015-06-10 15:58:50

标签: 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 } ?>

3 个答案:

答案 0 :(得分:1)

获取一些变量的帮助,如下面的

$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,不要认为这些是新的&amp;很难看,一看教程example

答案 1 :(得分:0)

我相信你的SQL语法需要一段时间:

$q=mysql_query("SELECT * FROM employees WHERE name LIKE '%.$query.%'" ) or die("could not search");

答案 2 :(得分:0)

我只会用这个:

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)){
        // ToDo
    }
}

供你参考:你做了mysql_query()两次:第一次是字符串,第二次是结果 - 第二次是错误。