即使用户未填写表单中的所有条件,也会从mySQL返回结果

时间:2012-07-20 16:14:24

标签: php mysql pdo

以下代码需要使用用户提供的3个变量。默认情况下,所有这些变量都等于0。

  1. 时间(文本框)
  2. city(下拉列表)
  3. type(下拉列表)
  4. 例如,如果用户给出了时间和城市,但是让类型为零,则不会返回任何结果。 我的问题是修改现有代码的有效方法是什么,如果用户选择不选择时间,城市或类型或这些的任何组合,会返回结果?

    例如,如果 time 21:00添加 city 编号3,它将显示符合2个条件的所有类型被列出。

    $question= 'SELECT * FROM events WHERE ABS(TIMESTAMPDIFF( HOUR , `time`, :time )) < 2 AND city=:city AND type=:type';
    $query = $db->prepare($question);
    $query->bindValue(":time", $time, PDO::PARAM_INT);
    $query->bindValue(":city", $city, PDO::PARAM_INT);
    $query->bindValue(":type", $type, PDO::PARAM_INT);
    $query->execute();
    

4 个答案:

答案 0 :(得分:1)

我更喜欢使用一系列条件,并检查条件是否存在,以构建SQL查询的各个部分:

$conditions = array(); // Creating an array of conditions.
if ($time) // Checks to see if value exists.
{
    $timeCondition = "ABS(TIMESTAMPDIFF( HOUR , `time`, :time )) < 2";
    $conditions[] = $timeCondition; // Adds this condition string to the array.
}
if ($city)
{
    $cityCondition = "city=:city";
    $conditions[] = $cityCondition;
}
if ($type)
{
    $typeCondition = "type=:type";
    $conditions[] = $typeCondition;
}

$conditionString = implode(" AND ", $conditions); // Gluing the values of the array with " AND " in between the string conditions.

if (count($conditions) > 0) // If conditions exist, add "WHERE " to the condition string.
{
    $conditionString = "WHERE ".$conditionString;
}
else // Otherwise, the condition string is blank by default.
{
    $conditionString = '';
}

$question= 'SELECT * FROM events '.$conditionString; // If no conditions, will return all from events. Otherwise, conditions will be slotted in through $conditionString.

$query = $db->prepare($question);

if($time)
    $query->bindValue(":time", $time, PDO::PARAM_INT);
if($city)
    $query->bindValue(":city", $city, PDO::PARAM_INT);
if($type)
    $query->bindValue(":type", $type, PDO::PARAM_INT);

$query->execute();

答案 1 :(得分:1)

<?php
$question= 'SELECT * FROM events WHERE ';

$hasTime = false;
if(!empty($time)) { // @note better validation here
    $hasTime = true;
    $question .= 'ABS(TIMESTAMPDIFF( HOUR , `time`, :time )) < 2 ';
}

$hasCity = false;
if(!empty($city)) { // @note better validation here
    $hasCity = true;
    $question .= 'AND city=:city ';
}

$hasType = false;
if(!empty($type)) { // @note better validation here
    $hasType = true;
    $question .= 'AND type=:type';
}

$query = $db->prepare($question);

if($hasTime)
    $query->bindValue(":time", $time, PDO::PARAM_INT);
if($hasCity)
    $query->bindValue(":city", $city, PDO::PARAM_INT);
if($hasType)
    $query->bindValue(":type", $type, PDO::PARAM_INT);

$query->execute();
$results = $query->fetchAll();

if(empty($results))
    echo 'no results';
else
    // $results is an array of arrays

答案 2 :(得分:0)

动态构建查询,以便在字段为默认字段时,不要将其包含在where子句中。

$conditions = array();
if ($_POST['time']) {
    $conditions[] = "ABS(TIMESTAMPDIFF( HOUR , `time`, :time )) < 2";
}
if ($_POST['city']) {
    $conditions[] = "city=:city";
}
if ($_POST['type']) {
    $conditions[] = "type=:type";
}

$conditionString = implode(" AND ", $conditions);
if (count($conditions) > 0) {
    $conditionString = "WHERE " . $conditionString;
}
else {
    $conditionString = '';
}
$question = 'SELECT * FROM events ' . $conditionString;

答案 3 :(得分:0)

您可以在SQL语句中使用一系列IF()语句,如果未设置该值,则返回true。所以像这样:

...WHERE IF(:time, ABS(TIMESTAMPDIFF(HOUR, `time`, :time)) < 2, 1)
AND IF(:city, city=:city, 1) AND IF(:type, type=:type, 1)