【问题标题】:Multiple Input Field Search trouble多输入字段搜索问题
【发布时间】:2015-01-02 09:03:02
【问题描述】:

我有一个搜索多个输入字段的搜索功能脚本。该脚本正在完美地搜索数据库。问题是当我使用 OR 运算符时,我必须输入两个要搜索的字段并且结果显示正常,但如果我只输入 1 个字段,则搜索不起作用并显示记录。如果我使用 AND 运算符,我必须输入 1 个字段并显示相应的结果,但是当我输入 2 个字段时,结果为 NULL。 AND & OR 运算符都给了我横向结果。

我希望当我输入 1 个字段时,应该显示匹配的结果,当我输入两个字段时,应该显示与这两个字段匹配的结果。

这是我的 index.php 搜索脚本。

//Perform Search from our database
if(isset($_POST['action_type']))
{
    echo '';
    if ($_POST['action_type'] == 'search')
    {
        $search = mysqli_real_escape_string($link, strip_tags($_POST['searchText']));
        $search_add = mysqli_real_escape_string($link, strip_tags($_POST['searchAddress']));

        $sql = "select contact_id, CONCAT(first_name,' ' , last_name) as contact_name,
                contact_no, residential_address, 
                company, company_address from tblcontact 
                where CONCAT(first_name, ' ', Last_name) like '%$search%' or contact_no like '%$search_add%'  order by contact_id asc";


        $result = mysqli_query($link, $sql);

        if(!$result)
        {
            echo mysqli_error($link);
            exit();
        }




        //Loop through each row on array and store the data to $contact_list[]
        while($rows = mysqli_fetch_array($result))
        {
            $contact_list[] = array('contact_id' => $rows['contact_id'], 
                                    'contact_name' => $rows['contact_name'],
                                    'contact_no' => $rows['contact_no'],
                                    'residential_address' => $rows['residential_address'],
                                    'company' => $rows['company'],
                                    'company_address' => $rows['company_address']);
        }
        include 'contactlist.php';
        exit();
    }
}

这是我的contactlist.php,它有表单。

<center><div style="margin-bottom: 5px;">
            <form method="POST" action="index.php" >
                <table>
                <tr>
                <th>Name</th>
                <td><input type="text" id="searchText" name="searchText" style="width: 300px; margin-left: 14px;"/></td>
                </tr>
                <tr>
                <th>Contact</th>
                <td><input type="text" id="searchAddress" name="searchAddress" style="width: 300px; margin-left: 14px;"/></td>
                </tr>
                <input type="hidden" name="action_type" value="search"/>
                </table><br>
                &nbsp;&nbsp;&nbsp;<input type="submit" value="Refresh Search" onClick="window.location.href='index.php'">


            </form>

            </div>
            <div style="max-height: 350px; overflow:auto;">
            <table class="pbtable" border="1">
                <thead>
                    <tr>
                        <th>
                            ID
                        </th>
                        <th>
                            Name
                        </th>
                        <th>
                            Contact #
                        </th>
                        <th>
                            Res. Address
                        </th>
                        <th>
                            Company
                        </th>
                        <th>
                            Company Address
                        </th>
                        <th></th><th></th>
                    </tr>
                </thead><br><br><br>
                <tbody>
                    <?php foreach($contact_list as $contact) : ?>
                        <tr>
                            <td>
                                <?php echo $contact["contact_id"]; ?>
                            </td>
                            <td>
                                <?php echo $contact["contact_name"]; ?>
                            </td>
                            <td>
                                <?php echo $contact["contact_no"]; ?>
                            </td>
                            <td>
                                <?php echo $contact["residential_address"]; ?>
                            </td>
                            <td>
                                <?php echo $contact["company"]; ?>
                            </td>
                            <td>
                                <?php echo $contact["company_address"]; ?>
                            </td>
                            <td>
                                <form method="post" action="index.php">
                                    <input type="hidden" name="ci" 
                                    value="<?php echo $contact["contact_id"]; ?>" />
                                    <input type="hidden" name="action" value="edit" />
                                    <input type="submit" value="Edit" />
                                </form> 
                            </td>
                            <td>
                                <form method="POST" action="index.php" 
                                onSubmit="return ConfirmDelete();">
                                    <input type="hidden" name="ci" 
                                    value="<?php echo $contact["contact_id"]; ?>" />
                                    <input type="hidden" name="action" value="delete" />
                                    <input type="submit" value="Delete" />
                                </form>
                            </td>
                        <tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
            </div>

如果输入了 1 个字段并且输入了 2 个字段,搜索应该会起作用。这个问题的解决方案是什么?

【问题讨论】:

  • 即使我最近也遇到了同样的问题..我使用if条件来克服它..我建议你也使用if条件..if($variable!=""){ //perform one query }elseif($variable2!==""){ //perform another query }
  • 请使用我的脚本显示它

标签: php mysql html mysqli


【解决方案1】:

您需要检查字段是否为空:

if(!empty($_POST['searchText'])
     $search = mysqli_real_escape_string($link, strip_tags($_POST['searchText']));
if(!empty($_POST['searchAddress'])
     $search_add = mysqli_real_escape_string($link, strip_tags($_POST['searchAddress']));

然后,根据此您应该更改查询。

if(!empty($_POST['searchText'] && !empty($_POST['searchAddress'])
{
   $sql = "select contact_id, CONCAT(first_name,' ' , last_name) as contact_name,
                contact_no, residential_address, 
                company, company_address from tblcontact 
                where CONCAT(first_name, ' ', Last_name) like '%$search%' or contact_no like '%$search_add%'  order by contact_id asc";
}
else if(empty($_POST['searchText'] && !empty($_POST['searchAddress'])
{
   $sql = "select contact_id, CONCAT(first_name,' ' , last_name) as contact_name,
                contact_no, residential_address, 
                company, company_address from tblcontact 
                where contact_no like '%$search_add%'  order by contact_id asc";
}
else if(!empty($_POST['searchText'] && empty($_POST['searchAddress'])
{
    $sql = "select contact_id, CONCAT(first_name,' ' , last_name) as contact_name,
                contact_no, residential_address, 
                company, company_address from tblcontact 
                where CONCAT(first_name, ' ', Last_name) like '%$search%' order by contact_id asc";
}

根据您尝试为 AND 案例实现的目标调整您的查询。

【讨论】:

    【解决方案2】:

    请试试这个版本的 sql 查询:

    $sql = "select contact_id, CONCAT(first_name,' ' , last_name) as contact_name,
                    contact_no, residential_address, 
                    company, company_address from tblcontact 
                    where CONCAT(first_name, ' ', Last_name) like '%"
                    .(empty($search) ? "" : ($search."%"))
                    ."' AND contact_no like '%"
                    .(empty($search_add) ? "" : ($search_add."%"))
                    ."'  order by contact_id asc";
    

    【讨论】:

    • 如果输入两个字段进行搜索,则会给出 NULL 值
    • 谢谢回复。您的表first_name, Last_name, contact_no 的任何字段是否有可能具有NULL 值?
    • Aris 的回答非常适合我。我只需要使用 If 和 Else 语句。
    • 很好 - 很高兴您得到了所需的帮助 :) 感谢您的回复,祝您在 2015 年新年万事如意。
    猜你喜欢
    • 2019-08-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多