多在哪里使用查询中的选择选项

时间:2014-12-11 15:53:28

标签: php mysql where

我想像这样进行多重选择

<form method="POST" action="">
<select name="age">
<option value="">Any</option>
    <option value="0">18-20</option>
    <option value="1">21-23</option>
    <option value="2">24-27</option>
    <option value="3">28-32</option>
    <option value="4">33-39</option>
    <option value="5">40-49</option>
    <option value="6">50+</option>
</select>
<select name="gender">
    <option value="">Any</option>
  <option value="male">Male</option>
  <option value="female">Female</option>
</select>
<input type="submit" name="filtrer" value="Submit">
</form>

我的查询就是这个

$retour_messages = mysql_query('SELECT * FROM users ORDER BY id DESC LIMIT '. mysql_real_escape_string ($premiereEntree) .', '. mysql_real_escape_string ($messagesParPage) .'');

如果有人可以帮助我,我需要通过我的选择对我的结果进行多重排序。

THX

3 个答案:

答案 0 :(得分:0)

尝试这样的事情。性别和年龄字段应在您的表格中

 $where = array();

if(isset($_POST['age']) && $_POST['age'] !== ''){
   $where[] = ' `age` = ' . (int)$_POST['age'];
}

if(isset($_POST['gender']) && $_POST['gender'] !== ''){
   $where[] = " `gender` = '" . mysql_real_escape_string(htmlspecialchars(trim($_POST['gender']))) . "' ";
}

$retour_messages = mysql_query('SELECT * FROM users ' . implode(' AND ', $where) . ' ORDER BY id DESC LIMIT '. (int)$premiereEntree .', '. (int)$messagesParPage .'');

答案 1 :(得分:0)

我应该推荐:

if ($_POST['filtrer']){
$age = $_POST['age'];
$gender = $_POST['gender'];

$age_ct = count($age);
$gender_ct = count($gender);

$i=0;
while($i < $age_ct){
    $age_SQL .= "'$age[$i]',";
    $i++;
}

if ($age_ct < 1){
    $age_SQL = " age IN('')";
}else{
    $age_SQL = eregi_replace(",$",'',$age_SQL);
    $age_SQL = " AND age IN($age_SQL)";
}

$i=0;
while($i < $gender_ct){
    $gender_SQL .= "'$gender[$i]',";
    $i++;
}

if ($gender_ct < 1){
    $gender_SQL = " gender IN('')";
}else{
    $gender_SQL = eregi_replace(",$",'',$gender_SQL);
    $gender_SQL = " AND gender IN($gender_SQL)";
}

$retour_messages = mysql_query("SELECT * FROM users WHERE 1=1 $age_SQL $gender_SQL ORDER BY id DESC LIMIT ". mysql_real_escape_string($premiereEntree).",".mysql_real_escape_string($messagesParPage)." ");

}

您需要使用<select name="age[]" multiple><select name="gender[]" multiple>

答案 2 :(得分:0)

您需要构建动态查询才能执行此操作。从基本存根开始

$sql = "SELECT * FROM users ";

然后你需要将initial子句设置为WHERE。

 $clause = " WHERE ";//Initial clause

检查是否设置了$_POST['filtrer']

if(isset($_POST['filtrer'])){//to run PHP script on submit

然后使用switch case

开始构建查询
//age
if(!empty($_POST['age'])){
    switch ($_POST['age']) {
    case 1:
        $sql .= "$clause (age >18 AND <20)";
        break;
    case 2:
        $sql .= "$clause (age >21 AND <23)";
        break;
    case 3:
        $sql .= "$clause (age >24 AND <27)";
        break;
    case 4:
        $sql .= "$clause (age >28 AND <32)";
        break;
    case 5:
        $sql .= "$clause (age >33 AND <39)";
        break;
    case 6:
        $sql .= "$clause (age >40 AND <49)";
        break;
    case 7:
        $sql .= "$clause age >50";
        break;      
}

将子句设置为AND

$clause = " AND ";// Set clause to AND 
}
//gender
if(!empty($_POST['gender'])){
    switch ($_POST['gender']) {
    case 'male':
        $sql .= "$clause gender = male";
        break;
    case 'female':
        $sql .= "$clause gender = female";
        break;
}
}
$sql .=" ORDER BY id DESC LIMIT";//. mysql_real_escape_string ($premiereEntree) .", ";  
echo $sql;
}

您需要重新编号年龄选项

<form method="POST" action="#">
<select name="age">
<option value="">Any</option>
    <option value="0">18-20</option>
    <option value="2">21-23</option>
    <option value="3">24-27</option>
    <option value="4">28-32</option>
    <option value="5">33-39</option>
    <option value="6">40-49</option>
    <option value="7">50+</option>
</select>

建议使用PDO或Mysqli,因为不推荐使用mysql_函数