使用多个搜索词进行搜索

tcomlyy6  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(322)

我想要一个搜索表格,搜索我的整个数据库。搜索表单应该由几个输入和选择标记组成。
atm在提交所有可能的列和行组合后显示给我。
以下是搜索fom部分:

<div style="width:1420px" class="wmfg_layout_2">
    <form action="xxx.php" method="post">
        <ul class="wmfg_questions">
            <li class="wmfg_q">
            <label class="wmfg_label">Suchkriterien auswählen</label>
                <table class="wmfg_answers">

                    <tr class="wmfg_a">
                    <th>

                        <td><label class="wmfg_label_a" for="chk_search1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;search1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label></td>
                        <td><input type="text" name="search1" size="25"></td>

        </th>
                    <th>

                        <td><label class="wmfg_label_a" for="chk_search2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;search2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label></td>
                        <td><input type="text" name="search2" size="25"></td>

        </th>
        <th>

                    <td><label class="wmfg_label_a" for="chk_search3">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Status&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label></td>
                    <td>

                    <select name="search3" size="1">
                    <option disabled selected value> </option>;

            <?php
            foreach($result as $m)
            {

            echo "<option value=\"" . $m['resultID'] . "\">" .$m['result'] . "</option>";

            }
        ?>

        </td>
        </select>

                    </th>

        <th>
                    <td><label class="wmfg_label_a" for="chk_search4">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;search4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label></td>
                    <td>

                    <select name="search4" size="1">
                    <option disabled selected value> </option>;

            <?php
            foreach($result2 as $m)
            {

            echo "<option value=\"" . $m['result2ID'] . "\">" .$m['result2'] . "</option>";

            }
        ?>

                    </th>
                    <th>

                    <td><label class="wmfg_label_a" for="chk_search5">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;search&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label></td>
                    <td>

                    <select name="search5" size="1">
                    <option disabled selected value> </option>;

            <?php
            foreach($result3 as $m)
            {

            echo "<option value=\"" . $m['resul3ID'] . "\">" .$m['result3'] . "</option>";

            }
        ?>

        </tr>
                    </th>

                    </table>
        </td>
        </select>

                    </th>
                            </li>
                            <li class='wmfg_qEnd'>
                                <input type='submit' name='suche' value='Anfrage senden'>
                            </li>
                        </ul>

                </tr>
    </form>

</div>

总是相同的结果,当我使用例如search和search1或仅search5等并提交它时,它会显示所有列和行组合作为结果,而不是我想要的行当我搜索我的关键字时,我认为我必须拆分搜索项并用条件回显它,但不知道如何。以下是php部分代码:

<?php
        $search1=$_POST['search1'];
        $search2=$_POST['search2'];
        $search3=$_POST['search3'];
        $search4=$_POST['search4'];
        $search5=$_POST['search5'];
        $search = $connection->query("SELECT col1, col2, col3, col4, col5, col5, col6, col7, col8 FROM table1, table2, table3
        WHERE table1.col1 LIKE '%".$search1."%'
        OR table1.col2 LIKE '%".$search2."%'
        OR table1.col3 LIKE '".$search3."'
        OR table1.col4 LIKE '".$search4."'
        OR table1.col5 LIKE '".$search5."'");

    $search->execute();
    $result = $search->fetchAll(PDO::FETCH_ASSOC);

    if (isset($_POST['search1'])
        or isset($_POST['search2'])
        or isset($_POST['search3'])
        or isset($_POST['search4'])
        or isset($_POST['search5'])){ 

            foreach ($result as $row2) {
                echo "<tr>";
                echo "<td>".$row2['col1']."</td>";
                echo "<td>".$row2['col2']."</td>";
                echo "<td>".$row2['col3']."</td>";
                echo "<td>".$row2['col4']."</td>";
                echo "<td>".$row2['col5']."</td>";
                echo "<td>".$row2['col6']."</td>";
                echo "<td>".$row2['col7']."</td>";
                echo "<td>".$row2['col8']."</td>";

                echo "</tr>";
            }
        }

    $connection = null;
?>

调用查询的php代码的第二部分。
这是表1的一个示例表,以列为单位缩减只是为了便于理解,它有更多的列和行。
表1

+-------+------+-----------+-----------+----------+
| PK_ID | name | FK_ID_TB2 | FK_ID_TB3 |  status  |
+-------+------+-----------+-----------+----------+
|     1 | TC   |         1 |         1 | on hold  |
|     2 | HTS  |         2 |         2 | finished |
|     3 | HTOL |         3 |         3 | on going |
+-------+------+-----------+-----------+----------+

这是表2

+-------+------------+
| PK2_ID| technology |
+-------+------------+
|     1 | bla        |
|     2 | bli        |
|     3 | blubb      |
+-------+------------+

这里是表3

+-------+------------+
| PK3_ID| assignment |
+-------+------------+
|     1 | assign1    |
|     2 | assign2    |
|     3 | assign3    |
+-------+------------+

我有5个机制来搜索条目它的一个过滤器-2个输入表单字段和3个选择。当我提交时,我想显示所有符合我输入条件的记录。
对于exmaple,当我搜索“tc”时,结果应该是:

+-------+------+-----------+-----------+----------+
| PK_ID | name | technology | FK_ID_TB3 |  status  |
+-------+------+-----------+-----------+----------+
|     1 | TC   |        bla |    assign | on hold  |
+-------+------+-----------+-----------+----------+

当我搜索“tc”和“on going”时,应该显示第1行和第3行等。。
这个问题可以理解吗?提前寻求帮助。祝您有个美好的一天。

inkz8wg9

inkz8wg91#

在这里,我更新了我的答案,您可以使用locate instedof%检查空值,检查代码如下:

if(!isset($_POST['search1']))
   $_POST['search1']='';

   if(!isset($_POST['search2']))
   $_POST['search2']='';

    if($_POST['search1'] == '')
             $search1='';
     else 
             $search1=" LOCATE('".$_POST['search1']."',t1.name)>0 OR "

  if($_POST['search2'] == '' )
             $search2='';
     else 
             $search2=" LOCATE('".$_POST['search2']."',t1.status)>0 OR "

                $search3=$_POST['search3'];
                $search4=$_POST['search4'];

    $connection->query("
    SELECT t1.PK_ID , t1.name , t2.technology, t3.assignment, t1.status FROM table1 t1, table2 t2, table3 t3
            WHERE 
              t1.FK_ID_TB2=t2.PK2_ID 
            AND
               t1.FK_ID_TB2=t3.PK3_ID 
            AND
            (
               ".$search1."
               ".$search2."
             t2.technology = '".$search3."'
            OR t3.assignment = '".$search4."'

            )");

这里的例子取决于你的价值观http://sqlfiddle.com/#!9/027509/1

相关问题