从php mysql中的单个表中搜索多个字段

时间:2013-06-28 10:14:10

标签: php mysql search-engine

我有一个名为current的表,该表中有几个字段,如post,experience,company等。

我想进行搜索,其中有三个选择框,一个显示来自post字段的帖子,第二个显示experience,第三个显示来自单个当前表的company

我希望如果用户从任何一个选择字段中选择,或者从所有选择字段中选择,则应该从表中搜索相应的数据并显示仅与搜索相关的结果。

为此,我写了代码,但它不起作用请帮助我找到我哪里出错,我无法理解。

它仅显示在post字段中输入的数据,但不显示在任何其他字段中输入的数据。

我的代码在这里

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="">
<table>

<tr>
<?php
$ss=mysql_query("select * from current"); 

?>
<td><select name="post"><?php while($rr=mysql_fetch_array($ss))
{?><option value="<?php echo $rr['post'];?>"><?php echo $rr['post'];?></option><?php } ?></select></td>
<?php
$s11=mysql_query("select * from current"); 

?>
<td><select name="experience"><?php while($r11=mysql_fetch_array($s11))
{?><option value="<?php echo $r11['experience'];?>"><?php echo $r11['experience'];?></option><?php } ?>    </select></td>
<td>
<?php
$sss=mysql_query("select * from current"); 

?>
<select name="company"><?php while($rrr=mysql_fetch_array($sss))
{?><option value="<?php echo $rrr['cname'];?>"><?php echo $rrr['cname'];?></option><?php } ?></select></td>
<td><input type="submit" name="search" value="Search" /></td>
</tr>

</table>
    </form>

我的搜索代码在这里

   <?php
include('Admin/config.php');
    error_reporting(E_ERROR | E_WARNING | E_PARSE);
  if(isset($_REQUEST['search']))
  {
$post = $_REQUEST['post'];
$ex = $_REQUEST['experience'];
$company = $_REQUEST['company'];
$query = "select * from current where post like '%$post%' or experience like '%$ex%' or cname like '%$company%' ";
$res1 = mysql_query($query);
while($rows = mysql_fetch_array($res1))
{
?>
<tr>
   <td><?php echo $rows['date'];?></td>
  <td><?php echo $rows['post'];?></td>
  <td><?php echo $rows['qualification'];?></td>
  <td><?php echo $rows['experience'];?></td>
  <td><?php echo $rows['nop'];?></td>
   <td><?php echo $rows['noj'];?></td>
  <td><?php echo $rows['cname'];?></td>
  <td><?php echo $rows['jloc'];?></td>


   </tr><?php

  }}

  else
  {


    $s=mysql_query("SELECT * FROM current ORDER BY date DESC");



  while($rows=mysql_fetch_array($s))
  {
  ?>
   <tr>
   <td><?php echo $rows['date'];?></td>
   <td><?php echo $rows['post'];?></td>
   <td><?php echo $rows['qualification'];?></td>
   <td><?php echo $rows['experience'];?></td>
   <td><?php echo $rows['nop'];?></td>
    <td><?php echo $rows['noj'];?></td>
   <td><?php echo $rows['cname'];?></td>
   <td><?php echo $rows['jloc'];?></td>


   </tr>
   <?php
   }}
   ?>
   </table>

1 个答案:

答案 0 :(得分:1)

<?php
include('Admin/config.php');
error_reporting(E_ERROR | E_WARNING | E_PARSE);
$ss=mysql_query("select `post`,`experience`,`cname` from current");
$filter = array();
while($rr = mysql_fetch_assoc($ss)) {
    foreach ($rr as $key => $val) {
        $filter[$key][] = $val;
    }
}
$request = array('post' => '', 'experience' => '', 'cname' => '');
if  (isset($_REQUEST['search']))
{
    $request = array(
        'post' => $_REQUEST['post'],
        'experience' => $_REQUEST['experience'],
        'cname' => $_REQUEST['cname'],
    );
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="">
    <table>
        <tr>

            <?php
            foreach ($filter as $name => $value) {
                ?>
                <td>
                    <select name="<?=$name?>">
                        <?
                        foreach ($value as $key => $val) {
                            ?>
                            <option value="<?=$val?>"<?= $val ===  $request[$name] ?  ' selected' : ''?>><?=$val?></option>
                        <?
                        }
                        ?>
                    </select>
                </td>
            <?
            }
            ?>
            <td><input type="submit" name="search" value="Search" /></td>
        </tr>
    </table>
</form>
<table>
    <?php
    if(isset($_REQUEST['search']))
    {
        $where = '';
        foreach ($request as $key => $val) {
            if ($where !== '') {
                $where .= ' OR ';
            }
            $where .= "`".$key."` like '%".$val."%' ";
        }
        $query = "select * from current where ".$where;
    }
    else
    {
        $query = "select * from current ORDER BY date DESC";
    }
    $res1 = mysql_query($query);
    while($rows=mysql_fetch_array($res1))
    {
        ?>
        <tr>
            <td><?php echo $rows['date'];?></td>
            <td><?php echo $rows['post'];?></td>
            <td><?php echo $rows['qualification'];?></td>
            <td><?php echo $rows['experience'];?></td>
            <td><?php echo $rows['nop'];?></td>
            <td><?php echo $rows['noj'];?></td>
            <td><?php echo $rows['cname'];?></td>
            <td><?php echo $rows['jloc'];?></td>
        </tr>
    <?php
    }
    ?>
</table>