多输入字段搜索问题

Multiple Input Field Search trouble

我有一个搜索功能脚本可以搜索多个输入字段。该脚本正在完美地搜索数据库。问题是当我使用 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>
                   <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 个字段,搜索应该会起作用。这个问题的解决方案是什么?

请尝试此版本的 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";

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

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 案例中尝试实现的目标调整您的查询。