使用PHP的网站搜索功能

时间:2014-07-04 03:20:28

标签: php html search mysqli

我是初学者,我尝试做农业应用,获得一些经验,并尝试一些先进的东西。但我无法弄清楚如何在我的应用程序中进行搜索。我有:

- 4 select html elements
- 5 search boxes

由于空间原因,我只会为html元素编写3/14列。

HTML code:

<form>
    <!-- makes the global filter based on all the data from db -->
    <select id="select1" name="select1" onchange="this.form.submit()">
        <option value="">Select</option>
        <option value="code">Code</option>
        <option value="land">Land</option>
        <option value="name">Name</option>
    </select><br/>

    <!-- makes a filter for a html column, based on a db column -->
    <select id="select2" name="select2" onchange="this.form.submit()">
        <option value="">All parcels</option>
        <option value="P1">P1</option>
        <option value="P2">P2</option>
        <option value="P3">P3</option>
    </select>

    <!-- search boxes to filter 5 html columns -->
    <input type="search" id="search1" name="search2">
    <input type="search" id="search2" name="search2">
    <input type="search" id="search3" name="search3">
    <input type="search" id="search4" name="search4">

    <!-- same as the previous select menu, just this is for another html column -->
    <select id="select3" name="select3" onchange="this.form.submit()">
        <option value="">Select</option>
        <option value="MO">MO</option>
        <option value="MA">MA</option>
        <option value="ME">ME</option>
    </select>

    <!-- the 5th search box -->
    <input type="search" id="search5" name="search5">

    <!-- renders the data from db according to the selections made by the user or search terms -->
    <table id="table" class="display" cellspacing="0" width="100%">
        <tr>
            <th id="code">Code</th>
            <th id="land">Land</th>
            <th id="name">Name</th>
        </tr>

        <tr>
            <?php
                require 'config.php'; # login to database
                # ---- QUERY ----
            ?>
        </tr>
    </table>

    <!-- this displays in the page as many rows as the user selects -->
    <select name="select4" id="select4" onchange="this.form.submit()">
        <option value="">Select</option>
        <option value="1">1</option>
        <option value="5">5</option>
        <option value="10">10</option>
    </select>
</form>

PHP代码:我只是从上面放了这段代码而不是“#---- QUERY ----”来填充页面中的html列:

if(isset($_POST['select1'])){
    $select1 = $_POST['select1'];
    switch($select1){
        case 'code':
            $stmt = $db->query("SELECT * FROM users WHERE `code`='code'");
            while($row = $stmt->fetch_assoc()){ ?>
                <tr class="action">
                    <td id="td"><?php echo $row['code'];?></td>
                    <td id="td"><?php echo $row['land'];?></td>
                    <td id="td2"><?php echo $row['name'];?></td>
                </tr> <?php
            }
        break; # some more cases follows
    } # end switch($select1)
} # end if(isset($_POST['select1']))
elseif(isset($_POST['select2'])){
    $select2 = $_POST['select2'];
    switch($select2){
        case P1:
            $stmt = $db->query("SELECT * FROM users WHERE `parcel` LIKE '%P1%'");
            while($row = $stmt->fetch_assoc()){ ?>
                <tr class="action">
                    <td id="td"><?php echo $row['code'];?></td>
                    <td id="td"><?php echo $row['land'];?></td>
                    <td id="td2"><?php echo $row['name'];?></td>
                </tr> <?php
            }
        break; # some more cases here
    } # end switch($select2)
} # end if(isset$_POST['select2'])
elseif(isset($_POST['search1'])){
    $search1 = $_POST['search1'];
    $stmt = $db->query("SELECT * FROM users WHERE `name` LIKE '%search1%'");
    while($row = $stmt->fetch_assoc()){ ?>
        <tr class="action">
            <td id="td"><?php echo $row['code'];?></td>
            <td id="td"><?php echo $row['land'];?></td>
            <td id="td2"><?php echo $row['name'];?></td>
        </tr> <?php
    }
} # end elseif(isset($_POST['search1']))
elseif(isset($_POST['search2'])){
    $search2 = $_POST['search2'];
    $stmt = $db->query("SELECT * FROM users WHERE `cont_ref_a` LIKE '%search2%'");
    while($row = $stmt->fetch_assoc()){ ?>
        <tr class="action">
            <td id="td"><?php echo $row['code'];?></td>
            <td id="td"><?php echo $row['land'];?></td>
            <td id="td2"><?php echo $row['name'];?></td>
        </tr> <?php
    }
} # end elseif(isset($_POST['search2']))
elseif(isset($_POST['search3'])){
    $search3 = $_POST['search3'];
    $stmt = $db->query("SELECT * FROM users WHERE `owner` LIKE '%search3%'");
    while($row = $stmt->fetch_assoc()){ ?>
        <tr class="action">
            <td id="td"><?php echo $row['code'];?></td>
            <td id="td"><?php echo $row['land'];?></td>
            <td id="td2"><?php echo $row['name'];?></td>
        </tr> <?php
    }
} # end elseif(isset($_POST['search3']))
elseif(isset($_POST['search4'])){
    $search4 = $_POST['search4'];
    $stmt = $db->query("SELECT * FROM users WHERE `block` LIKE '%search4%'");
    while($row = $stmt->fetch_assoc()){ ?>
        <tr class="action">
            <td id="td"><?php echo $row['code'];?></td>
            <td id="td"><?php echo $row['land'];?></td>
            <td id="td2"><?php echo $row['name'];?></td>
        </tr> <?php
    }
} # end elseif(isset($_POST['search4']))
elseif(isset($_POST['select3'])){
    $select3 = $_POST['select3'];
    switch($select3){
        case 'T1':
            $stmt = $db->query("SELECT * FROM users WHERE `zone`='T1'");
            while($row = $stmt->fetch_assoc()){ ?>
                <tr class="action">
                    <td id="td"><?php echo $row['code'];?></td>
                    <td id="td"><?php echo $row['land'];?></td>
                    <td id="td2"><?php echo $row['name'];?></td>
                </tr> <?php
            }
        break; # end case 'T1', more cases follows
    } # end switch($select3)
} # end elseif(isset($_POST['select3']))
elseif (isset($_POST['search5'])){
    $search5 = $_POST['search5'];
    $stmt = $db->query("SELECT * FROM users WHERE `s_a` LIKE '%search5%'");
    while($row = $stmt->fetch_assoc()){ ?>
        <tr class="action">
            <td id="td"><?php echo $row['code'];?></td>
            <td id="td"><?php echo $row['land'];?></td>
            <td id="td2"><?php echo $row['name'];?></td>
        </tr><?php
    }
} # end elseif(isset($_POST['search5']))
elseif(isset($_POST['select1'])){
    $select1 = $_POST['select1'];
    switch($select1){
        case 'code':
            $stmt = $db->query("SELECT * FROM users");
            while($row = $stmt->fetch_assoc()){ ?>
                <tr class="action">
                    <td id="td"><?php echo $row['code'];?></td>
                </tr><?php
            }
        break; # end case 'code', more cases follows
    } # end switch($select1)
} # end elseif(isset($_POST[select1]))
elseif(isset($_POST['select4'])){
    $select4 = $_POST['select4'];
    switch($select4){
        case 1:
            $stmt = $db->query("SELECT * FROM users LIMIT 1");
            while($row = $stmt->fetch_assoc()){ ?>
                <tr class="action">
                    <td id="td"><?php echo $row['code'];?></td>
                    <td id="td"><?php echo $row['land'];?></td>
                    <td id="td2"><?php echo $row['name'];?></td>
                </tr><?php
            }
        break; # end case 1, more cases follows
    } # end switch($select4)
} # end elseif(isset($_POST['select4'])))
else { # if noone of the select menus or search boxes where filled, then just render all the data from db to the page
    $stmt = $db->query("SELECT * FROM users");
    while($row = $stmt->fetch_assoc()){ ?>
        <tr class="action">
            <td id="td"><?php echo $row['code'];?></td>
            <td id="td"><?php echo $row['land'];?></td>
            <td id="td2"><?php echo $row['name'];?></td>
        </tr><?php
    }
} # end php script

很抱歉这篇文章很长,但我试着尽可能地表达。问题是,当我从选择菜单中选择一个选项,或者我输入要搜索的术语时,不会显示任何内容。我现在不在乎,如果我的代码中出现安全问题,我只想让这个应用程序工作。有帮助吗?非常感谢!


LE:我终于找到了解决问题的方法(我会在这里写一下,这样任何其他人都会搜索这类问题,得到一个答案(我会写一个简单的例子)): / p>

对于搜索框:

<form action="" method="post">
    <input type="search" name="search1" onchange="this.form.submit()">
    <table>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
        </tr><?php
            if(isset($_POST['search1'])){
                $search1 = $_POST['search1'];
                if(!empty($_POST['search1'])){
                    $sql = "SELECT * FROM tblName WHERE fname LIKE '%$search1%'";
                    $stmt = $db->query($sql);
                    while($row = $stmt->fetch_assoc()){ ?>
                        <tr>
                            <td><?php echo $row['fname'];?></td>
                            <td><?php echo $row['lname'];?></td>
                        </tr><?php
                    }
                }
            }
            else {
                $sql = "SELECT * FROM tblName";
                $stmt = $db->query($sql);
                while($row = $stmt->fetch_assoc()){ ?>
                    <tr>
                        <td><?php echo $row['fname'];?></td>
                        <td><?php echo $row['lname'];?></td>
                    </tr><?php
                }
            } ?>
    </table>
</form>

对于选择菜单:

<form action="" method="post">
    <select name="select1" onchange="this.form.submit()">
        <option value="">Select</option>
        <option value="fname">First Name</option>
        <option value="lname">Last Name</option>
    </select>
    <table>
        <tr>
            <th>First Name</th>
            <th>Last Name</th>
        </tr><?php
            if(isset($_POST['select1'])){
                $select1 = $_POST['select1'];
                if(!empty($_POST['select1'])){
                    switch($select1){
                        case 'fname':
                            $stmt = $db->query("SELECT * FROM users");
                            while($row = $stmt->fetch_assoc()){ ?>
                                <tr>
                                    <td><?php echo $row['fname'];?></td>
                                </tr><?php
                            }
                        break;
                        case 'lname':
                            $stmt = $db->query("SELECT * FROM users");
                            while($row = $stmt->fetch_assoc()){ ?>
                                <tr>
                                    <td><?php echo $row['lname'];?></td>
                                </tr><?php
                            }
                        break;
                    }
                }
            }
            else {
                $stmt = $db->query("SELECT * FROM users");
                while($row = $stmt->fetch_assoc()){ ?>
                    <tr>
                        <td><?php echo $row['fname'];?></td>
                        <td><?php echo $row['lname'];?></td>
                    </tr><?php
                }
            }?>
    </table>
</form>

1 个答案:

答案 0 :(得分:0)

您的示例代码不完整。它的编写方式,你的许多搜索选项将无法正常工作。我会尝试选择select2和P1。这应该有用。

但是所有的IF语句都可能存在错误。你不应该只测试是否设置了$ _POST变量,你应该确保它不是NULL或零长度(strlen($ var)&gt; 0)。发布变量可以“设置”但仍未提交实际数据。

我会删除示例中与select2无关的所有内容,看看是否可以让它以更简单的格式工作。

当然,用一堆HTML粉碎你的所有SQL和PHP代码是一个坏主意,但这不是问题的一部分。