使用4个不同的选择框搜索数据库

时间:2015-08-07 20:01:55

标签: php mysql sql search

我有一个相当复杂的“WHERE”语句来创建。我搜索了许多网站以找到答案,但我的页面仍然没有显示任何内容。

业务要求:使用4个不同的下拉菜单,通过选择所有四个中的一个,全部或任意组合来从数据库中获取结果。

以下是searchInstructors.php文件的php代码:

         <?php
        $dsn = 'mysql: host=localhost; dbname=name';
        $user = 'user';
        $password = 'pass';
        try {
        $pdo = new PDO($dsn, $user, $password);
        $pdo ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch (PDOException $e) {
        echo 'Connection failed: ' . $e->getMessage();
        }

         if(isset($_GET['semester'])){
            $semester = $_GET['semester'];  
        }else{
            $semester = "Not Selected";
        }
        if(isset($_GET['year'])){
            $year = $_GET['year'];  
        }else{
            $year = "Not Selected";
        }
        if(isset($_GET['coursePrefix'])){
            $coursePrefix = $_GET['coursePrefix'];  
        }else{
            $coursePrefix = "Not Selected";
        }
        if(isset($_GET['courseNumber'])){
            $courseNumber = $_GET['courseNumber'];  
        }else{
            $courseNumber = "Not Selected";
        }

        echo "<table class='tg' width='100%'>";
        echo "<tr>

                <th><strong>Course</strong></th>        
                <th><strong>Name</strong></th>
                <th><strong>Email/Phone</strong></th>
                <th><strong>Semester</strong></th>
                <th><strong>Institution</strong></th>
        </tr>";
        $sql="SELECT courses.coursePrefix  courses.courseNumber,   facultyContact.firstName, facultyContact.lastName, facultyContact.email, facultyContact.phone, facultyContact.institution, courses.semester, courses.year
        FROM courses
        LEFT JOIN facultyContact 
        ON courses.ID=facultyContact.ID
        WHERE (courses.semester LIKE :semester AND courses.year LIKE :year)
OR (courses.semester LIKE :semester AND courses.coursePrefix LIKE :coursePrefix)
OR (courses.semester LIKE :semester AND courses.courseNumber LIKE :courseNumber) 
OR (courses.year LIKE :year AND courses.coursePrefix LIKE :coursePrefix)
OR (courses.year LIKE :year AND courses.courseNumber LIKE :courseNumber)
OR (courses.coursePrefix LIKE :coursePrefix AND courses.courseNumber LIKE :courseNumber)
OR (courses.semester LIKE :semester AND courses.year LIKE :year AND courses.coursePrefix LIKE :coursePrefix)
OR (courses.semester LIKE :semester AND courses.year LIKE :year AND courses.courseNumber LIKE :courseNumber)
OR  (courses.semester LIKE :semester AND courses.year LIKE :year AND courses.coursePrefix LIKE :coursePrefix AND courses.courseNumber LIKE :courseNumber)
        ORDER BY facultyContact.lastName";

        $stmt = $pdo->prepare($sql);
        $stmt->bindParam(':semester', $semester, PDO::PARAM_STR);
        $stmt->bindParam(':year', $year, PDO::PARAM_INT);
        $stmt->bindParam(':coursePrefix', $coursePrefix, PDO::PARAM_INT);
        $stmt->bindParam(':courseNumber', $courseNumber, PDO::PARAM_INT);
        $stmt->execute();
        $total = $stmt->rowCount();
        while ($row = $stmt->fetchObject()) {

           echo "<tr>
                <td>{$row->coursePrefix}<br /> {$row->courseNumber} </td>
                <td>{$row->firstName} {$row->lastName}</td>
                <td>{$row->email} <br />{$row->phone}</td>
                <td>{$row->semester} {$row->year} </td>
                <td>{$row->institution} </td>";
        }
        if (empty($total)) { 
            echo '<td colspan="5">No results found for your selection<br>' .
        '<strong>'. $semester . ' - ' . $year. ' - ' .  $coursePrefix. ' - ' .     $courseNumber . '</strong></td>'; 
        } else {
            echo implode($total);
        }

        $pdo = null;
        echo "</tr></table>";
           ?>

以下是search_form.php中的HTML代码:

                <div class="search-wrapper">
         <form id="form2" method="get" action="searchInstructors.php"> 
            <p> 
            <label for="semester">Semester</label>
                <select name="semester" id="semester">
                  <option selected="selected" value="Not Selected">Select One</option>
                  <option value="Spring">Spring</option>
                  <option value="Summer">Summer</option>
                  <option value="Fall">Fall</option>
              </select>
        </p>
        <p>
            <label for="year">Year</label>
                <select name="year" id="year">
                  <option selected="selected" value="Not Selected">Select One</option>
                  <option value="2015">2015</option>
                  <option value="2014">2014</option>
                  <option value="2013">2013</option>
                </select>
            </p>
            <p>
            <label for="coursePrefix">Course Subject</label> 
             <select name="coursePrefix" id="coursePrefix">
               <option selected="selected" value="Not Selected">Select One</option>
               <option value="ACED">ACED</option>
               <option value="CRJU">CRJU</option>
               <option value="ENGL">ENGL</option>
               <option value="FREN">FREN</option>
               <option value="HADM">HADM</option>
               <option value="LEAS">LEAS</option>
               <option value="ORGL">ORGL</option>
               <option value="PLA">PLA</option>
               <option value="POLS">POLS</option>
               <option value="PSYC">PSYC</option>
               <option value="SOCI">SOCI</option>
               <option value="SPAN">SPAN</option>
             </select>
        </p>
        <p>

    <label for="courseNumber">Course Number</label> 
     <select name="courseNumber" id="courseNumber">
       <option selected="selected" value="Not Selected">Select One</option>
       <option value="1100">1100</option>
       <option value="1101">1101</option>
       <option value="1102">1102</option>
       <option value="2000">2000</option>
       <option value="2010">2010</option>
       <option value="2050">2050</option>
       <option value="2100">2100</option>
       <option value="2200">2200</option>
       <option value="2300">2300</option>
       <option value="2400">2400</option>
       <option value="2700">2700</option>
       <option value="3000">3000</option>
       <option value="3001">3001</option>
       <option value="3002">3002</option>
       <option value="3010">3010</option>
       <option value="3030">3030</option>
       <option value="3050">3050</option>
       <option value="3100">3100</option>
       <option value="3101">3101</option>
       <option value="3110">3110</option>
       <option value="3150">3150</option>
       <option value="3160">3160</option>
       <option value="3200">3200</option>
       <option value="3201">3201</option>
       <option value="3210">3210</option>
       <option value="3220">3220</option>
       <option value="3230">3230</option>
       <option value="3240">3240</option>
       <option value="3250">3250</option>
       <option value="3260">3260</option>
       <option value="3300">3300</option>
       <option value="3301">3301</option>
       <option value="3302">3302</option>
       <option value="3303">3303</option>
       <option value="3304">3304</option>
       <option value="3350">3350</option>
       <option value="3400">3400</option>
       <option value="3500">3500</option>
       <option value="3501">3501</option>
       <option value="3600">3600</option>
       <option value="3610">3610</option>
       <option value="3700">3700</option>
       <option value="3710">3710</option>
       <option value="3800">3800</option>
       <option value="3810">3810</option>
       <option value="4000">4000</option>
       <option value="4001">4001</option>
       <option value="4002">4002</option>
       <option value="4011">4011</option>
       <option value="4020">4020</option>
       <option value="4050">4050</option>
       <option value="4070">4070</option>
       <option value="4110">4110</option>
       <option value="4160">4160</option>
       <option value="4200">4200</option>
       <option value="4210">4210</option>
       <option value="4220">4220</option>
       <option value="4230">4230</option>
       <option value="4240">4240</option>
       <option value="4250">4250</option>
       <option value="4251">4251</option>
       <option value="4260">4260</option>
       <option value="4300">4300</option>
       <option value="4301">4301</option>
       <option value="4350">4350</option>
       <option value="4401">4401</option>
       <option value="4402">4402</option>
       <option value="4500">4500</option>
       <option value="4600">4600</option>
       <option value="4610">4610</option>
       <option value="4620">4620</option>
       <option value="4650">4650</option>
       <option value="4690">4690</option>
       <option value="4700">4700</option>
       <option value="4800">4800</option>
       <option value="4802">4802</option>
       <option value="4820">4820</option>
       <option value="4860">4860</option>
       <option value="4900">4900</option>
       <option value="4950">4950</option>
       <option value="4960">4960</option>
       <option value="4980">4980</option>
       <option value="4991">4991</option>
     </select>
      </p>
      <div class="clear"></div>
      <input type="submit" name="search" id="search" value="Search" class="btn" />
 </form>
 </div>

这不起作用。所以我尝试了这个:

 if(isset($_GET['semester'])){
 $semester = $_GET['semester'];
 $query="SELECT courses.coursePrefix, courses.courseNumber, facultyContact.firstName, facultyContact.lastName, facultyContact.email, facultyContact.phone, facultyContact.institution, courses.semester, courses.year
FROM courses
LEFT JOIN facultyContact 
ON courses.ID=facultyContact.ID
WHERE courses.semester LIKE :semester OR courses.year LIKE :year OR  courses.coursePrefix LIKE :coursePrefix OR courses.courseNumber LIKE :courseNumber
ORDER BY facultyContact.lastName";
 }else{
    $semester = "Not Selected";
 }
 if(isset($_GET['year'])){
     $year = $_GET['year'];
    $query="SELECT courses.coursePrefix, courses.courseNumber, facultyContact.firstName, facultyContact.lastName, facultyContact.email, facultyContact.phone, facultyContact.institution, courses.semester, courses.year
FROM courses
LEFT JOIN facultyContact 
ON courses.ID=facultyContact.ID
WHERE courses.semester LIKE :semester OR courses.year LIKE :year OR courses.coursePrefix LIKE :coursePrefix OR courses.courseNumber LIKE :courseNumber
ORDER BY facultyContact.lastName";
 }else{  
    $year = "Not Selected";
 }
 if(isset($_GET['coursePrefix'])){
     $coursePrefix = $_GET['coursePrefix'];
    $query="SELECT courses.coursePrefix, courses.courseNumber, facultyContact.firstName, facultyContact.lastName, facultyContact.email, facultyContact.phone, facultyContact.institution, courses.semester, courses.year
FROM courses
LEFT JOIN facultyContact 
ON courses.ID=facultyContact.ID
WHERE courses.semester LIKE :semester OR courses.year LIKE :year OR courses.coursePrefix LIKE :coursePrefix OR courses.courseNumber LIKE :courseNumber
ORDER BY facultyContact.lastName";
 }else{
   $coursePrefix = "Not Selected";
 }
 if(isset($_GET['courseNumber'])){
     $courseNumber = $_GET['courseNumber'];
    $query="SELECT courses.coursePrefix, courses.courseNumber, facultyContact.firstName, facultyContact.lastName, facultyContact.email, facultyContact.phone, facultyContact.institution, courses.semester, courses.year
FROM courses
LEFT JOIN facultyContact 
ON courses.ID=facultyContact.ID
WHERE courses.semester LIKE :semester OR courses.year LIKE :year OR courses.coursePrefix LIKE :coursePrefix OR courses.courseNumber LIKE :courseNumber
ORDER BY facultyContact.lastName";
 }else{  
   $courseNumber = "Not Selected";
 }

它也没用。当WHERE语句是全部AND和所有OR时,我可以使它工作。它可以使用两个下拉和一个或像这样:

courses.semester LIKE :semester AND courses.year LIKE :year OR 
courses.semester LIKE :semester AND courses.coursePrefix LIKE :coursePrefix OR
courses.semester LIKE :semester AND courses.courseNumber LIKE :courseNumber

我曾考虑创建视图,只是让每个下拉视图,但是有太多的组合,当添加新课程时,我将不得不重做视图。 是否可以创建一个WHERE语句或查询,以任意组合方式从4个下拉列表中获取输入?

2 个答案:

答案 0 :(得分:3)

不要为每个可能的学期,年份,前缀和数字组合手动创建SQL,而是让PHP动态构建它。

定义您希望在SQL中使用的所有变量。

$inputs = array(
    'semester'      => false,
    'year'          => false,
    'coursePrefix'  => false,
    'courseNumber'  => false
);

检查$ _GET数组中是否存在变量。

foreach ( $inputs as $k => $v ) {
    if ( isset($_GET[$k]) ) {
        $inputs[$k] = $_GET[$k];
    }
}

在SQL中动态构建where子句。注意:我的代码是一个起点,并不能完成您需要的所有操作,例如绑定。你自己必须这样做。

$sql = 'SELECT your_columns FROM courses LEFT JOIN your_joins ';
$where = array();
foreach ( $inputs as $k => $v ) {
    if ( $v !== false ) {
        $where[$k] = 'courses.'.$k.' = :'.$k;   
    }
}
if ( sizeof($where) > 0 ) {
    $sql .= 'WHERE '.implode(' AND ', $where);  
}
$sql .= ' ORDER BY some_columns';

现在玩一些网址,例如

semester=Summer&year=2009&coursePrefix=PLA
semester=Summer&year=2009
semester=Summer

并注意代码如何确切地知道或关注给出的参数组合。

一旦你解决了这个问题,添加允许用户指定和/或参数之间的选项 - 或允许他们选择倍数(例如复选框)非常容易。

答案 1 :(得分:0)

因此,经过多长时间的研究后,我的代码才有效。所以这适用于其他所有必须做复杂事情的人。

<?php
$dsn = 'mysql: local host; dbname=dbName';
$user = 'user';
$password = 'pass;
try {
$pdo = new PDO($dsn, $user, $password);
$pdo ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
try{
    $query = "SELECT courses.coursePrefix, courses.courseNumber, facultyContact.firstName, facultyContact.lastName, facultyContact.email, facultyContact.phone, facultyContact.institution, courses.semester, courses.year
FROM courses 
LEFT JOIN facultyContact 
ON courses.ID=facultyContact.ID ";
$cond = array();
$params = array();

 if( isset($_GET['semester']) && !(empty($_GET['semester'])) ){  
    $cond[] = " courses.semester LIKE '". $_GET['semester']."'";
    $params[] = $semester;

}
if(isset($_GET['year']) && !(empty($_GET['year'])) ){  
    $cond[] = " courses.year LIKE '". $_GET['year']."'";
    $params[] = $year;
}
if(isset($_GET['coursePrefix']) && !(empty($_GET['coursePrefix'])) ){
    $cond[] = " courses.coursePrefix LIKE '". $_GET['coursePrefix']."'";
    $params[] = $coursePrefix;
}
if(isset($_GET['courseNumber']) && !(empty($_GET['courseNumber'])) ){
    $cond[] = " courses.courseNumber LIKE '". $_GET['courseNumber'] ."'";
    $params[] = $courseNumber;
}
if (count($cond)) {
    $query .= ' WHERE ' . implode(' AND ', $cond);
}
$query.= "ORDER BY facultyContact.lastName, facultyContact.firstName";

$stmt = $pdo->prepare($query);
$stmt->execute($params);

echo "<table class='tg' width='100%'>";
echo "<tr>

        <th><strong>Course</strong></th>        
        <th><strong>Name</strong></th>
        <th><strong>Email/Phone</strong></th>
        <th><strong>Semester</strong></th>
        <th><strong>Institution</strong></th>
</tr>";

$total = $stmt->rowCount();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC) ) {

   echo "<tr>    
        <td>" . $row['coursePrefix'] . " ". $row['courseNumber'] . "</td>" .

        "<td>" . $row['firstName'] . " " . $row['lastName'] . "</td>" .

        "<td>" . $row['email']. "<br />" . $row['phone'] ."</td>" .

        "<td>" . $row['semester'] ." " . $row['year'] . "</td>" .

        "<td>" . $row['institution'] . "</td></tr>";
}
//user error message for no results
if (empty($total)) { 
    echo '<td colspan="5">No results found for your selection<br>' .
'<strong>'. $_GET['semester'] . ' - ' . $_GET['year']. ' - ' .  $_GET['coursePrefix'] . ' - ' . $_GET['courseNumber'] . '</strong></td>'; 
} else {
    echo implode($total);
}
} catch (PDOException $e) {
    $error_messageALL = $e->getMessage();
    echo "DB error: " . $error_messageALL;  

    }

    $pdo = null;
    echo "</tr></table>";
       ?>