搜索&匹配数据库中的数据然后显示结果

时间:2016-06-15 07:02:07

标签: php search mysqli

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>

我正在使用此代码从表中搜索。我必须填4个字段来获取一行数据。我怎样才能填充两个或三个字段&amp;匹配数据库和数据库中的列搜索数据(我们在各种工作门户中搜索的方式)??在此先感谢_ / _

2 个答案:

答案 0 :(得分:2)

您可以根据用户输入

创建搜索模式
<?php

if (isset($_REQUEST['submit'])) {
    $desg = $_POST['desg'];
    $cities = $_POST['cities'];
    $exp = $_POST['exp'];
    $prof = $_POST['prof'];
    $sql = "SELECT * FROM adjob WHERE ";
    $flag = FALSE;

    if (isset($desg) && $desg != "") {// condition for desg
        if (!$flag) {
            $or = "";
        } else {
            $or = "OR";
        }
        $sql.=" $or desg like '%" . $desg . "%'";
        $flag = TRUE;
    }
    if (isset($cities) && $cities != "") { // condition for cities
        if (!$flag) {
            $or = "";
        } else {
            $or = "OR";
        }
        $sql.=" $or desg like '%" . $cities . "%'";
        $flag = TRUE;
    }
    if (isset($exp) && $exp != "") {// condition for exp
        if (!$flag) {
            $or = "";
        } else {
            $or = "OR";
        }
        $sql.=" $or desg like '%" . $exp . "%'";
        $flag = TRUE;
    }
    if (isset($prof) && $prof != "") {// condition for prof
        if (!$flag) {
            $or = "";
        } else {
            $or = "OR";
        }
        $sql.=" $or desg like '%" . $prof . "%'";
        $flag = TRUE;
    }
    $q = mysqli_query($con, $sql);
} else {
    $sql = "SELECT * FROM adjob";
    $q = mysqli_query($con, $sql);
}

答案 1 :(得分:0)

希望这对你有用

if(isset($_REQUEST['submit']))
{
$desg    = isset($_POST['desg']) ? $_POST['desg'] : '';
$cities  = isset($_POST['cities']) ? $_POST['cities'] : '';
$exp     = isset($_POST['exp']) ? $_POST['exp'] : ''; 
$prof    = isset($_POST['prof']) ? $_POST['prof'] : '';
$sql     = "SELECT * FROM adjob WHERE desg like '%".$desg."%' OR desg like '%".$desg."' OR desg like '".$desg."%' OR cities like '%".$cities."%' OR cities like'".$cities."%' OR cities like '%".$cities."' OR exp like '%".$exp."%' OR exp like '".$exp."%'  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>