使用PHP MYSQL通过关键字搜索

时间:2019-03-27 23:50:40

标签: php mysql search

我想通过类似客户问题的关键字进行搜索。但是,每次搜索时,它都会提取每个条目,而不是来自其问题的条目。

我尝试不对每个对象使用,而只是做一个简单的操作,但这也不起作用。

这是正在传递的内容。

<form id="" class="searchbar" action="searchAppt.php" method="get">
           <input type="text" name="terms" size="40" class = "sbar" required value="Search by keyword" oninput="validity.valid||(value='');"
                       onblur="if (this.value == '') {
                                    this.value = 'Enter keyword';
                                }"
                       onfocus="if (this.value == 'Enter keyword') {
             this.value = '';
         }"/>&nbsp;&nbsp;
                <button type="submit" class = "btn">Search</button>
            </form>

if (filter_has_var(INPUT_GET, "terms")) {
    $terms_str = filter_input(INPUT_GET, 'terms', FILTER_SANITIZE_NUMBER_INT);
} else {
    echo "There were no appointments found.";
    include ('includes/footer.php');
    exit;
//explode the search terms into an array
            $terms = explode(" ", $terms_str);


            $sql = "SELECT * FROM appointments WHERE 1";
            foreach ($terms as $term) {
             $sql .= " AND issue LIKE '%$term%' AND email = '". $_SESSION['email'] ."' 
                ";
            }

            $result = $conn->query($sql);

            if ($result->num_rows > 0) {
                echo "<br /><br /><center><h1>Your Ticket(s)</h1><br />
                <div class='table'>
                <div class='tr'>
                <div class='td'><b>Ticket #</b></div>
                <div class='td'><b>First Name</b></div>
                <div class='td'><b>Last Name</b></div>
                <div class='td'><b>Phone #</b></div>
                <div class='td'><b>Building</b></div>
                <div class='td'><b>Room #</b></div>
                <div class='td'><b>Issue</b></div>
                <div class='td'><b>Appt. Start Time</b></div>
                <div class='td'><b>Appt. End Time</b></div>
                <div class='td'><b>Ticket Details</b></div>
                </div>";
                // output data of each row
                while($row = $result->fetch_assoc()) {
                    echo "<div class='tr'>
                    <div class='td'>".$row["id"]."</div>
                    <div class='td'>".$row["fname"]."</div>
                    <div class='td'>".$row["lname"]."</div>
                    <div class='td'>".$row["phonenum"]."</div>
                    <div class='td'>".$row["building"]."</div>
                    <div class='td'>".$row["room"]."</div>
                    <div class='td'>".$row["issue"]."</div>
                    <div class='td'>".$row["start_time"]."</div>
                    <div class='td'>".$row["end_time"]."</div>
                    <div class='td'><form action='ticketdetails.php' method='post'>
                        <input type='hidden' name='id' value='".$row["id"]."'>
                        <input type='submit' value='Ticket Details'></form>
                    </div>
                    </div>";
                }
                echo "</div>";
            } else {
                echo "<br /> <br />Your search <i>'$terms_str'</i> did not match any appointments";

它显示每个约会,而不是出现与关键字搜索类似的问题的约会。我觉得我只是用select语句忽略了一些东西。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您的问题是您打给filter_input的电话。您正在指定FILTER_SANITIZE_NUMBER_INT的过滤器,但传递的是空格分隔的单词列表。结果,filter_input的输出是一个空字符串(因为带有该标志,它会去除输入中除+/-和数字外的所有内容),因此不会在查询中添加任何条件。您可能想改用FILTER_SANITIZE_STRING