0

我有一个名为 current 的表,该表中有几个字段,如职位、经验、公司等。

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

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

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

它只显示在帖子字段中输入的数据,而不显示在任何其他字段中输入的数据。

我的代码在这里

<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>
4

1 回答 1

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>
于 2013-06-28T11:03:15.910 回答