使用PHP过滤多个下拉菜单选项

时间:2016-04-08 15:03:01

标签: php sql filtering

编辑:我现在已经尝试再次执行我的PHP代码了,它现在有效,但除了打印所有课程,每次按提交而不是过滤器的课程。此外,我似乎无法在过滤时将两个表连接在一起。教练的名字来自不同的教学指导员,我想选择这个。这是我的PHP代码。

<?php
include('connect-db.php');

if($_POST){
  if($_POST['Days'] == 'Monday') {
      $query = "SELECT * FROM lesson WHERE Day='Monday'";
  }elseif($_POST['Days'] == 'Tuesday') {
      $query = "SELECT * FROM lesson WHERE Day='Tuesday'";
  }elseif($_POST['Days'] == 'Wednesday') {
      $query = "SELECT * FROM lesson WHERE Day='Wednesday'";
  }elseif($_POST['Days'] == 'Thursday') {
      $query = "SELECT * FROM lesson WHERE Day='Thursday'";
  }elseif($_POST['Days'] == 'Friday') {
      $query = "SELECT * FROM lesson WHERE Day='Friday'";
  }elseif($_POST['Days'] == 'Saturday') {
      $query = "SELECT * FROM lesson WHERE Day='Saturday'";
  }elseif($_POST['Days'] == 'Sunday') {
      $query = "SELECT * FROM lesson WHERE Day='Sunday'";
  }elseif($_POST['Days'] == 'All') {
      $query = "SELECT * FROM lesson";
  }

  if($_POST['Instructors'] == 'Trevor') {
      $query = "SELECT * FROM lesson INNER JOIN instructor 
      ON lesson.Instructor_ID = instructor.Instructor_ID WHERE Forename='Trevor'";
  }elseif($_POST['Instructors'] == 'Laura') {
      $query = "SELECT * FROM lesson INNER JOIN instructor 
      ON lesson.Instructor_ID = instructor.Instructor_ID WHERE Forename='Laura'";
  }elseif($_POST['Instructors'] == 'Rachel') {
      $query = "SELECT * FROM lesson INNER JOIN instructor 
      ON lesson.Instructor_ID = instructor.Instructor_ID WHERE Forename='Rachel'";
  }elseif($_POST['Instructors'] == 'Ryan') {
      $query = "SELECT * FROM lesson INNER JOIN instructor 
      ON lesson.Instructor_ID = instructor.Instructor_ID WHERE Forename='Ryan'";
  }elseif($_POST['Instructors'] == 'Steve') {
      $query = "SELECT * FROM lesson INNER JOIN instructor 
      ON lesson.Instructor_ID = instructor.Instructor_ID WHERE Forename='Steve'";
  }elseif($_POST['Instructors'] == 'All') {
      $query = "SELECT * FROM lesson INNER JOIN instructor 
      ON lesson.Instructor_ID = instructor.Instructor_ID";
  }

  if($_POST['TypeLessons'] == 'Keeping Fit') {
      $query = "SELECT * FROM lesson WHERE Type_of_Lesson='Keeping Fit'";
  }elseif($_POST['TypeLessons'] == 'How to Swim') {
      $query = "SELECT * FROM lesson WHERE Day='How to Swim'";
  }elseif($_POST['TypeLessons'] == 'Relaxing Sessions') {
      $query = "SELECT * FROM lesson WHERE Day='Relaxing Sessions'";
  }elseif($_POST['TypeLessons'] == 'Being Sporty') {
      $query = "SELECT * FROM lesson WHERE Day='Being Sporty'";
  }elseif($_POST['TypeLessons'] == 'All') {
      $query = "SELECT * FROM lesson";
  }

  $result = mysql_query($query);

  if(!$result) {
  echo 'Could not get data: ' . mysql_error();
  }

 while($row = mysql_fetch_array($result)) {
  echo "Lesson ID: {$row["Lesson_ID"]} <br> " .
       "Name: {$row["Name"]} <br> " .
       "Day: {$row["Day"]} <br> " .
       "Start Date: {$row["Start_Date"]} <br> " .
       "Start Time: {$row["Start_Time"]} <br> " .
       "End Time: {$row["End_Time"]} <br> " .
       "Instructor: {$row["Forename"]} <br> " .
       "Type of Lesson: {$row["Type_of_Lesson"]} <br> " .
       "Number of Places: {$row["No_of_Places"]} <br> <br> ";
}

}
?> 

我一直在尝试过滤多个下拉菜单选项,以便在用户点击“搜索课程”时查询出现。但由于某种原因,我不断收到相同的错误消息:注意:未定义的索引:天。 即使PHP代码应该提取它,它似乎并不是,我无法弄明白。这是我下面的代码,注意我还没有完成所有PHP的选择。 另外,在选择PHP后选择这些选项时,如何打印这些查询。非常感谢你。

<form action="tester_filter.php" method="post">
<select name="Days">
<option value="All" selected="selected">All days</option>
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
<option value="Saturday">Saturday</option>
<option value="Sunday">Sunday</option>
</select>
<select name="Instructors">
<option value="All" selected="selected">All instructors</option>
<option value="Trevor">Trevor</option>
<option value="Laura">Laura</option>
<option value="Rachel">Rachel</option>
<option value="Ryan">Ryan</option>
<option value="Steve">Steve</option>
</select>
<select name="TypeLessons">
<option value="All" selected="selected">All types</option>
<option value="Keeping Fit">Keeping Fit</option>
<option value="How to Swim">How to Swim</option>
<option value="Relaxing Sessions">Relaxing Sessions</option>
<option value="Being Sporty">Being Sporty</option>
</select>
<input type="submit" value="search lessons" name="submit" />
</form>

3 个答案:

答案 0 :(得分:1)

<?php
if($_POST){
  if($_POST['Days'] == 'Monday') {
      $query = "SELECT * FROM lesson WHERE Day='Monday'";
  }elseif($_POST['Days'] == 'Tuesday') {
      $query = "SELECT * FROM lesson WHERE Day='Tuesday'";
  }
}
?> 

尝试这个

答案 1 :(得分:0)

修复通知错误使用以下代码。

stdio.h

或者您也可以通过以下代码检查

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
  if($_POST['Days'] == 'Monday') {
    $query = "SELECT * FROM lesson WHERE Day='Monday'";
  }
  elseif($_POST['Days'] == 'Tuesday') {
    $query = "SELECT * FROM lesson WHERE Day='Tuesday'";
  }
}

答案 2 :(得分:0)

假设您的PHP脚本位于 tester_filter.php 中,就像表单所示(您实际上没有在那里说明),我不确定您的内容是什么期待离开,天被捡起,但你只是将一个名为Query的变量设置为一个字符串,而不是用它做任何事情。

在每个if语句中,进行调试

if($_POST['Days'] == 'Monday') {
//A check we're having the variable pass through 
echo "this should say monday";
$query = "SELECT * FROM lesson WHERE Day='Monday'";
}

如果您正在尝试检查您的选择是否良好(看起来没问题),请选择星期一并查看是否输出了回音,然后您看起来不错。

您不能在没有连接到数据库的情况下分配查询,您需要一个连接字符串,例如:

//Connection variables
$servername = "localhost";
$username = "root";
$password = "password";
$database = "database";
//Connect to MySQL
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
else{ 
    echo "Connected successfully";
}

然后,一旦你拥有了这个基础,就可以使用你在$ Query中完成的工作来开始查询数据库。

对我来说解释可能太多了,所以我会留下尽可能多的有用链接,因为我认为你需要在途中帮助你。

Connect to a MySQL Database from PHP

Pulling data from a MySQL Database, Querying the Database through PHP

List of MySQLi functions and what they do

Another list of MySQLi functions and what they do

Another guide on how to select information from a MySQL Database

祝你好运,如果你有任何问题请发表评论。