简体   繁体   English

从MySQL返回的结果相同-我知道为什么,但不知道解决方案

[英]Same results returned from MySQL - I know why but don't know solution

What I am trying to achieve: I am trying to create a filter which students can use to filter tutors on my website by level, gender or subject. 我要实现的目标:我正在尝试创建一个过滤器,学生可以用来按级别,性别或主题过滤我网站上的导师。 To do this I have created check-boxes which when submitted as "ON" or checked a query is run to find tutors that fufill that criteria. 为此,我创建了一个复选框,当这些复选框提交为“ ON”或检查查询时,它会运行以查找符合该条件的导师。 I have used a while loop to check which check-boxes have been checked. 我使用了while循环来检查选中了哪些复选框。

What is happening: The query is returning the right results but say a tutor teaches Maths and English then he will be returned to the results div twice. 发生了什么:查询返回正确的结果,但是说一位导师教数学和英语,然后他将两次返回结果div。 This is because the tutor fulfills both queries and is returned twice. 这是因为导师完成两个查询,并且返回两次。

I don't know how to go about fixing this so thanks in advance and I'd appreciate all help, below is the relevant section of the HTML and PHP coding. 我不知道如何解决此问题,因此在此先感谢,感谢您的帮助,以下是HTML和PHP编码的相关部分。

<form action="" method="get" >
  Name: 
    <input type="text" name="tutor_name"><br>
  Level:
    <input type="checkbox" name="check[]" value="gcse">GCSE
    <input type="checkbox" name="check[]" value="a_level">A Level<br>
  Gender:
    <input type="checkbox" name="check[]" value="male">Male
    <input type="checkbox" name="check[]" value="female">Female<br>
  Subject:
    <input type="checkbox" name="check[]" value="maths">Maths
    <input type="checkbox" name="check[]" value="science">Science
    <input type="checkbox" name="check[]" value="english">English<br>
  <input type="submit" value="Go!">

<?php

if(!empty($_GET['check'])){
  foreach($_GET['check'] as $check) {

  $check = mysql_query("SELECT `tutor_id` FROM `tutors` WHERE $check=1");

  while($row = mysql_fetch_array($check)){
    $tutor_data = tutor_data($row['tutor_id'], 'first_name','image','rating','bio','last_name');

    echo "<br><h3> " . $tutor_data['first_name'];
    echo " " . $tutor_data['last_name'];
    echo "</h3><br>" . $tutor_data['bio'];
    echo "<br>Rating: " . $tutor_data['rating']. "<br><br>";
    }
  }
    $number= mysql_num_rows($check);
      echo "<b>$number</b> results";
}

What you should have is a single query that simple checks for multiple values at once: 您应该有一个查询,一次简单地一次检查多个值:

SELECT DISTINCT ...
WHERE 1 in (maths, english, male, a_level)

Right now you're running multiple queries independently of each other, so it stands to reason you'll get a duplicate tutor for every tutor that has two or more of the attributes you're searching for. 现在,您正在彼此独立地运行多个查询,因此,有理由为每个具有两个或多个要搜索的属性的导师获得一个重复的导师。 If you can't rewrite to the single-query model, then you'll have to fetch the results of each individual query, then combine them in PHP to filter out the dupes. 如果您无法重写为单查询模型,则必须获取每个查询的结果,然后将其组合到PHP中以过滤重复项。

Also, note that your query as written is vulnerable to SQL injection attacks . 另外,请注意,您编写的查询容易受到SQL注入攻击的攻击 Do not use that code on a production system until you've fixed this problem. 在解决此问题之前,请勿在生产系统上使用该代码。

You could try: 您可以尝试:

$check = mysql_query("SELECT `tutor_id` FROM `tutors` WHERE $check=1 GROUP BY `tutor_id`");

EDIT: 编辑:

Or maybe a SELECT DISTINCT... could work 或者也许是SELECT DISTINCT...可以工作

Try this 尝试这个

if(!empty($_GET['check'])){
  $chks = array(); //Declare Array
  foreach($_GET['check'] as $check) {
    $chks[] = "$check=1"; // Add 'checkboxvalue=1' to the array
  }
  $check = implode(' OR ',$chks); // Join the array with ' OR '
  $check = mysql_query("SELECT `tutor_id` FROM `tutors` WHERE $check");

    $number= mysql_num_rows($check);
      echo "<b>$number</b> results";

  while($row = mysql_fetch_array($check)){
    $tutor_data = tutor_data($row['tutor_id'], 'first_name','image','rating','bio','last_name');

    echo "<br><h3> " . $tutor_data['first_name'];
    echo " " . $tutor_data['last_name'];
    echo "</h3><br>" . $tutor_data['bio'];
    echo "<br>Rating: " . $tutor_data['rating']. "<br><br>";
    }

}

Here i am building up the where clause in the loop and executing single query. 在这里,我在循环中建立where子句并执行单个查询。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM