使用php搜索表单以匹配所有单词和特定单词

时间:2014-08-25 13:30:49

标签: php search mysqli

我有一个简单的搜索表单,使用select标签,可以搜索数据库中与特定列匹配的单词。但是,当他们输入任何单词时,我似乎无法搜索所有列。例如,如果在动物出现的任何地方选择所有并输入动物,它将抓住它。我觉得它应该是相当直接但似乎无法绕过它。非常感谢您提前感谢任何帮助。

<form id="search_form" method="POST" name="searchTeachers">
        <fieldset>
            <div>
            <label for="mebook-category">Search teachers by:</label>
            <select name="teacher-category">
                <option value="">All</option>
              <option value="school_name">School</option>
              <option value="school_board">School Board</option>
              <option value="country">Country</option>
              <option value="state">Province/State</option>
              <option value="city">City</option>
            </select>
            </div>
            <input type="text" name="keyword-search" value="<?php echo $keyword_search ?>">
        </fieldset>
        <fieldset>
            <button type="submit" name="search" value="search"><span>Search</span></button>
        </fieldset>
    </form>

// PHP

SELECT * FROM members_teachers WHERE $teacher_category LIKE CONCAT('%',?,'%') AND approved = '2'
$stmt->bind_param('s', $keyword_search)  or die('Try Again');
/* Execute it */
$stmt->execute();
/*Store Results*/
$stmt->store_result();
/* Get Number of Rows */
$num_of_rows = $stmt->num_rows;
/* Bind the Results */
$stmt->bind_result($teacher_id, $username, $password1, $password2, $email, $first_name, $last_name, $school_name, $school_board, $city, $country, $state, $occupation, $expertise1, $expertise2, $expertise3, $focus, $description, $avatar, $website, $facebook, $linkedin, $skype, $twitter, $approved, $date);

1 个答案:

答案 0 :(得分:0)

在搜索“全部”时,您似乎需要单独的查询,因为您的查询仅检查一个数据库字段。只需测试“全部”并使用“或”

将查询扩展到所有字段
SELECT * FROM members_teachers
WHERE (school_name LIKE CONCAT('%',?,'%')
       OR school_board LIKE CONCAT('%',?,'%')
       OR country LIKE CONCAT('%',?,'%')
       OR state LIKE CONCAT('%',?,'%')
       OR city LIKE CONCAT('%',?,'%')
)
AND approved = '2'

我担心你还需要绑定其他参数,直到mysqli允许命名参数(没有检查)。