【问题标题】:Search & match data from database then show result从数据库中搜索和匹配数据然后显示结果
【发布时间】:2016-06-15 07:02:06
【问题描述】:
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 个字段才能获取一行的数据。我如何填写两个或三个字段并匹配数据库和搜索数据中的列(我们在各种工作门户中搜索的方式)?提前谢谢_/_

【问题讨论】:

    标签: php search mysqli


    【解决方案1】:

    您可以根据用户输入创建搜索模式

    <?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);
    }
    

    【讨论】:

    • 是的!如果用户只选择两个输入或一个输入,它也可以工作
    • 但是当我输入两个或三个输入时,它显示错误.. :(
    • Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given 我在这里遇到这个错误&lt;?php while($res=mysqli_fetch_array($q)) { ?&gt;
    • 我应该回显它的数据还是只回显 $sql; ??
    • 在mysqli_query之前回显$sql!!
    【解决方案2】:

    希望这对你有用

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-23
      • 2018-01-15
      • 2015-08-12
      • 1970-01-01
      • 1970-01-01
      • 2017-11-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多