使用多个输入框搜索MySQL数据

时间:2015-01-01 11:26:32

标签: javascript php jquery html vb.net

我正在开发一个项目,该项目有一个搜索字段,用于搜索和显示单个输入字段中的数据。但我想创建多个输入搜索字段,如:

名称: 地址: 联系: [搜索按钮]

如果用户只填写1个字段,则应显示匹配结果,但如果填充2字段同时包含名称和地址,则应显示与这两个字段匹配的结果,如果填充所有字段,则应显示。我当前的脚本可以正常搜索一个搜索框,但我遇到问题,使其成为一个多搜索框搜索脚本。

这是我搜索数据的index.php。

<?php error_reporting(0);
include 'DBConfig.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']));
        $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%'";

        $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();
    }
}

//Insert or Update contact information
if(isset($_POST['action_type']))
{
    if ($_POST['action_type'] == 'add' or $_POST['action_type'] == 'edit')
    {
        //Sanitize the data and assign to variables
        $contact_id = mysqli_real_escape_string($link, strip_tags($_POST['ContactID']));
        $fname = mysqli_real_escape_string($link, strip_tags($_POST['fname']));
        $lname = mysqli_real_escape_string($link, strip_tags($_POST['lname']));
        $contact_no = mysqli_real_escape_string($link, strip_tags($_POST['ContactNo']));
        $ResAddress = mysqli_real_escape_string($link, strip_tags($_POST['ResAddress']));
        $Company = mysqli_real_escape_string($link, strip_tags($_POST['Company']));
        $CompAddress = mysqli_real_escape_string($link, strip_tags($_POST['CompAddress']));

        if ($_POST['action_type'] == 'add')
        {
            $sql = "insert into tblcontact set 
                    first_name = '$fname',
                    last_name = '$lname',
                    contact_no = '$contact_no',
                    residential_address = '$ResAddress',
                    company = '$Company',
                    company_address = '$CompAddress'";
        }else{
            $sql = "update tblcontact set 
                    first_name = '$fname',
                    last_name = '$lname',
                    contact_no = '$contact_no',
                    residential_address = '$ResAddress',
                    company = '$Company',
                    company_address = '$CompAddress' 
                    where contact_id = $contact_id";
        }


        if (!mysqli_query($link, $sql))
        {
            echo 'Error Saving Data. ' . mysqli_error($link);
            exit(); 
        }
    }
    header('Location: contactlist.php');
    exit();
}
//End Insert or Update contact information

//Start of edit contact read
$gresult = ''; //declare global variable
if(isset($_POST["action"]) and $_POST["action"]=="edit"){
    $id = (isset($_POST["ci"])? $_POST["ci"] : '');
    $sql = "select contact_id, first_name, last_name,
            contact_no, residential_address, 
            company, company_address from tblcontact 
            where contact_id = $id";

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

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

    $gresult = mysqli_fetch_array($result);

    include 'update.php';
    exit();
}
//end of edit contact read

//Start Delete Contact
if(isset($_POST["action"]) and $_POST["action"]=="delete"){
    $id = (isset($_POST["ci"])? $_POST["ci"] : '');
    $sql = "delete from tblcontact 
            where contact_id = $id";

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

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

}
//End Delete Contact

//Read contact information from database
$sql = "select contact_id, CONCAT(first_name,' ' , last_name) as contact_name,
        contact_no, residential_address, 
        company, company_address from tblcontact";

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

if(!$result)
{
    echo mysqli_error($link);
    exit();
}
//Loo 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,显示数据。

<?php 
include_once 'index.php';
?>
<!DOCTYPE html>
<html>
<head>
    <title>Phonebook</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
function ConfirmDelete(){
    var d = confirm('Do you really want to delete data?');
    if(d == false){
        return false;
    }
}
</script>
</head>
<body>
    <div class="wrapper">
        <div class="content" >
            <?php include 'header.php'; ?><br/>
            <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>
                <input type="hidden" name="action_type" value="search"/><input type="submit" value="Search">
                </table>



            </form>

            </div>
            <div style="max-height: 350px; overflow:auto;">
            <table class="pbtable">
                <thead>
                    <tr>
                        <th>
                            Name
                        </td>
                        <th>
                            Contact #
                        </th>
                        <th>
                            Res. Address
                        </th>
                        <th>
                            Company
                        </th>
                        <th>
                            Company Address
                        </th>
                        <th></th><th></th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach($contact_list as $contact) : ?>
                        <tr>
                            <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>
            <br/>
            <a href="update.php" class="link-btn">Add Contact</a>
        </div>
    </div>
</body>
</html>

如何进行多输入字段搜索。

3 个答案:

答案 0 :(得分:2)

  

//在你的SQL查询中

     

$ search_address = $ _ POST ['searchAddress'];

     

$ sql =“选择contact_id,CONCAT(first_name,'',last_name)为   联系人姓名,                   contact_no,residential_address,                   公司,company_address来自tblcontact                   其中CONCAT(first_name,'',Last_name)喜欢'%$ search%'和                   residential_address = '%$ SEARCH_ADDRESS%'“;

答案 1 :(得分:0)

SELECT * FROM tableName WHERE (field1 = 'searchBox1Value' OR field1 = 'searchBox2Value');

答案 2 :(得分:0)

$where  = "";
if($search1 != '')
{
    $where  = "CONCAT(first_name, ' ', Last_name) like '%$search1%'";
}   
if($search2 != '')
{
    $where += "or residential_address like '%$search2%'";
}       
$sql = "select * from tblcontact where ".$where."";