两个下拉菜单和一个按钮

I want to ask about this code. I have two dropdown menu and one button. I want to search in sql database what I choose in those drop down menu. What is the sql syntax for search item in sql database by using two drop down menu.

my database = test

Table = student

name  |   class    |  sex   | mark |
John  |   Five     | Male   | 75
Jashi |   Four     | Female | 89   |



##HTML##
<form action="search2.php" method="post">
<select name="class">
<option value="" selected="selected">Class</option>


</select>
<select name="sex">
<option value="" selected="selected">Sex</option>


</select>
<input type="submit" value="search" />
</form>

search2.php

    <?php
   mysql_connect('localhost', 'root', '');
   mysql_select_db ("test");

    $whereClauses = '';
    $class = count($_POST['class']);
    $sex = count($_POST['sex']);
    $i = 0;
    if (! empty($_POST['class'])) {
    foreach ($_POST['class'] as $class) {
    $whereClauses .="class='".mysql_real_escape_string($class)."'";
    if ($i++ == $class) {
    $whereClauses .= " AND";
    }
    }
    }
    if (! empty($_POST['sex'])) {
    foreach ($_POST['sex'] as $sex) {
    $whereClauses .="sex='".mysql_real_escape_string($sex)."'";
    }
    if ($i++ == $sex) {
    $whereClauses .= " AND";
    }
    }
    $sql = "SELECT * FROM student '".$where."' ORDER BY id DESC '".$limit."'";
    $result=mysql_query($sql);
    while ($row = mysql_fetch_array($result)) {
    echo $row['class'];
    echo $row['sex'];
    echo $row['mark'];
    }
    ?>


    ANY HELP WOULD BE APPRECIATED

If I got it right and you want to search for students by class and sex (gender?) you can use this SQL query:

SELECT * FROM `student` WHERE `class` = 'the_class' AND `sex` = 'the_sex';

Where:

  • the_class is the input from your choose Class dropdown
  • the_sex is the input from your choose Sex dropdown

If I understand it correctly, you want the input of two drop down menu's to define the Mysql query? In that case, you can simply first check whether the user has selected two values, and then you could change your query accordingly by adding the "AND" to the Mysql Query.

For example:

if(isset($_GET["class"]) && isset($_GET["sex"])) {
$result = mysql_query("SELECT * from student Where sex  = '". $_GET["sex"]."' AND  class = '".$_GET["class"]."'");

}

Of course, in the above mentioned solution I presume that you are aware how to POST and GET data through html forms and PHP.