Php developer
Php developer

Reputation: 446

search multiple fields from a single table in php mysql

I have a table named current with several fields in that table like post, experience, company, etc.

I want to make a search having three select boxes one showing the posts from post field, 2nd showing the experience and 3rd showing the company from a single current table.

I want that if user selects from any one select field or from all it should search the respective data from the table and show the results only related to search.

For this I have written the code but its not working please help me in finding where did I went wrong, I cant understand.

It only shows data entered in post field but not in any other field.

My code goes here

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

my search code goes here

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

Upvotes: 0

Views: 5482

Answers (1)

bbldzr
bbldzr

Reputation: 151

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

Upvotes: 1

Related Questions