简体   繁体   中英

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. I have used a while loop to check which check-boxes have been checked.

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. 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.

<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.

Also, note that your query as written is vulnerable to SQL injection attacks . 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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