使用php中的多个关键字搜索数据库

时间:2014-11-06 06:22:45

标签: php mysql sql mysqli

我有一个页面index.php,其中有三个关键字,用户可以根据这些关键字搜索数据库。此页面的代码是

<section id="services" class="emerald">
        <div class="container">
            <form class="form-horizontal" role="form" action="search.php" enctype="multipart/form-data" method="post">
                <div class="row">
                    <div class="col-md-4 col-sm-6">
                        <div class="media">
                            <div class="media-body">
                                <?php
                                    $servername = "xyz.com";
                                    $username = "xyz";
                                    $password = "xyz123";
                                    $dbname = "xyz";

                                    // Create connection
                                    $con = mysqli_connect($servername, $username, $password, $dbname);
                                    // Check connection
                                    if (!$con) {
                                        die("Connection failed: " . mysqli_connect_error());
                                    }

                                    $sql = "SELECT treatment_type FROM treatment_type";
                                    $result = $con->query($sql);
                                    echo "<label for='treatment_type'>Treatment Type: </label>";
                                    echo "<select name='treatment_type' id='treatment_type' class='form-control'>";
                                    while($row = $result->fetch_assoc()) {
                                    echo "<option value='" . $row['treatment_type'] . "'>" . $row['treatment_type'] . "</option>";
                                    }
                                    echo "</select>";
                                ?>
                            </div>
                        </div>
                    </div>


                    <div class="col-md-4 col-sm-6">
                        <div class="media">
                            <div class="media-body">
                                <?php
                                    $servername = "xyz.com";
                                    $username = "xyz";
                                    $password = "xyz123";
                                    $dbname = "xyz";

                                    // Create connection
                                    $con = mysqli_connect($servername, $username, $password, $dbname);
                                    // Check connection
                                    if (!$con) {
                                        die("Connection failed: " . mysqli_connect_error());
                                    }

                                    $sql = "SELECT course FROM course";
                                    $result = $con->query($sql);
                                    echo "<label for='course'>Course/Conference Type: </label>";
                                    echo "<select name='course' id='course' class='form-control'>";
                                    while($row = $result->fetch_assoc()) {
                                    echo "<option value='" . $row['course'] . "'>" . $row['course'] . "</option>";
                                    }
                                    echo "</select>";
                                ?>

                            </div>
                        </div>
                    </div><!--/.col-md-4-->

                    <div class="col-md-4 col-sm-6">
                        <div class="media-body">
                            <?php
                                $servername = "xyz.com";
                                $username = "xyz";
                                $password = "xyz123";
                                $dbname = "xyz";

                                // Create connection
                                $con = mysqli_connect($servername, $username, $password, $dbname);
                                // Check connection
                                if (!$con) {
                                    die("Connection failed: " . mysqli_connect_error());
                                }

                                $sql = "SELECT facilities FROM facilities";
                                $result = $con->query($sql);
                                echo "<label for='facilities'>Facilities: </label>";
                                echo "<select name='facilities' id='facilities' class='form-control'>";
                                while($row = $result->fetch_assoc()) {
                                echo "<option value='" . $row['facilities'] . "'>" . $row['facilities'] . "</option>";
                                }
                                echo "</select>";
                            ?>


                        </div>
                    </div>

                    <div class="col-md-4 col-sm-6">
                        <div class="media-body">
                            <div class="col-md-8">
                                <input class="btn btn-primary" value="Search" type="submit" name="submit">

                            </div>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </section>

search.php的代码

<?php
error_reporting(0);

$con=mysqli_connect("xyz.com","xyz","xyz123","xyz");
// Check connection
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$treatment_type = mysqli_real_escape_string($con, $_POST['treatment_type']);
$course = mysqli_real_escape_string($con, $_POST['course']);
$facilities = mysqli_real_escape_string($con, $_POST['facilities']);

$sql1 = "SELECT * FROM office WHERE keywords LIKE '%$treatment_type' and '%$course' and '%$facilities'";
$result = mysqli_query($con, $sql1);

if (mysqli_num_rows($result) > 0) 
    {
        while($row = mysqli_fetch_assoc($result)) {
        echo "Office name: " . $row["office_name"]. " - Location: " . $row["office_address"]. " " . $row["office_city"]. "<br>";
    }
} else {
    echo "0 results";
}
mysqli_close($con);
?>

我想要做的是,当用户点击搜索链接时,用户会被重定向到另一个页面searchresult.php,其中会显示所有可用详细信息的列表。但是当我运行脚本时,即使数据库中存在该值,我也会得到值0结果。

1 个答案:

答案 0 :(得分:1)

searchresult.php

中尝试此查询
$sql1 = "SELECT * FROM office WHERE keywords LIKE '%$treatment_type%' 
or keywords LIKE '%$course%' or keywords LIKE '%$facilities%'";