Pooojaaaa Pooojaaaa - 5 months ago 11
PHP Question

Search & match data from database then show result

if(isset($_REQUEST['submit']))
{
$desg=$_POST['desg'];
$cities=$_POST['cities'];
$exp=$_POST['exp'];
$prof=$_POST['prof'];
$sql="SELECT * FROM adjob WHERE desg like '%".$desg."%' OR cities like '%".$cities."%' OR exp like '%".$exp."%' OR prof like '%".$prof."%'";
$q=mysqli_query($con, $sql);
}
else
{
$sql="SELECT * FROM adjob";
$q=mysqli_query($con, $sql);
}
?>
<form method="post">
<table width="200" border="1">
<tr>
<td>Desgination</td>
<td><input type="text" name="desg" value="" /></td>
<td>City</td>
<td><input type="text" name="cities" value="" /></td>
<td>Experince</td>
<td><input type="text" name="exp" value="" /></td>
<td>Profile</td>
<td><input type="text" name="prof" value="" /></td>
<td><input type="submit" name="submit" value="Find" /></td>
</tr>
</table>
</form>
<table>
<tr>
<td>Desg</td>
<td>Cities</td>
<td>Exp</td>
<td>Prof</td>
</tr>
<?php
while($res=mysqli_fetch_array($q)){
?>
<tr>
<td><?php echo $res['desg']; ?></td>
<td><?php echo $res['cities']; ?></td>
<td><?php echo $res['exp']; ?></td>
<td><?php echo $res['prof']; ?></td>
</tr>
<?php }?>
</table>


I am using this code to search from the table. I have to the fill 4 fields to fetch the data of one row .. How can i fill two or three fields & match the columns from database & search data (the way we search in various job portals) ?? Thanks in advance _/_

Answer

You can create your search pattern according to user input as

 if (isset($_REQUEST['submit'])) {
    $desg = $_POST['desg'];
    $cities = $_POST['cities'];
    $exp = $_POST['exp'];
    $prof = $_POST['prof'];
    $sql = "SELECT * FROM adjob WHERE ";
    if (isset($desg) && $desg != "") {// condition for desg
        $sql.=" desg like '%" . $desg . "%'";
    }
    if (isset($cities) && $cities != "") { // condition for cities
        $sql.=" desg like '%" . $cities . "%'";
    }
    if (isset($exp) && $exp != "") {// condition for exp
        $sql.=" desg like '%" . $exp . "%'";
    }
    if (isset($prof) && $prof != "") {// condition for prof
        $sql.=" desg like '%" . $prof . "%'";
    }
    $q = mysqli_query($con, $sql);
} else {
    $sql = "SELECT * FROM adjob";
    $q = mysqli_query($con, $sql);
}