-2

我遇到了这个问题,我正在努力想出最好和最有效的解决方案。我有一个 php 网站,有 19 个不同的输入,所有这些都是可选的,我正在使用 get 方法。有了这些输入,我必须找到与数据库匹配的信息并将其返回。我遇到的问题是创建可以工作的查询。

$query = "SELECT * FROM TEST.table";
if(($_GET['Transmission_Line_Designation'] ==="") && ($_GET['Switch_Number'] === "") && ($_GET['Telecom_Circuit_Number'] === "")
    && ($_GET['Transmitter_Frequency'] === "") && ($_GET['Receiver_Frequency'] === "") && ($_GET['power_level'] === "")
    && ($_GET['Phase'] === "") && ($_GET['Modulate'] === "") && ($_GET['trap_type'] === "") &&  ($_GET['line_tuner_type'] === ""))
    //checks if there were no input, if there werent any, display all columns and rows from the data base
{

}
    else
{

这就是我遇到问题的地方,如果用户输入第一个输入,它将起作用,但是,如果用户将第一个输入留空,则出现的查询是“在哪里和”某事,我在想使用大量 if 循环来检查条件并添加标志,但我很确定有一种更简单的方法可以做到这一点。

        $query.= " where ";
        if
        $query .= ($_GET['Transmission_Line_Designation'] === "") ? '' :  'Line_Designation = "'.$_GET['Transmission_Line_Designation'].'"';
        $query .= ($_GET['Switch_Number'] === "") ? '' :  ' and Switch_Number = "'.$_GET['Switch_Number'].'"';
        $query .= ($_GET['Telecom_Circuit_Number'] === "") ? '' :  ' and Telecom_Circuit_Number = "'.$_GET['Telecom_Circuit_Number'].'"';
        $query .= ($_GET['Transmitter_Frequency'] === "") ? '' :  ' and Transmitter_Frequency = "'.$_GET['Transmitter_Frequency'].'"';
        $query .= ($_GET['Receiver_Frequency'] === "") ? '' :  ' and Receiver_Frequency = "'.$_GET['Receiver_Frequency'].'"';
        $query .= ($_GET['power_level'] === "") ? '' :  ' and power = "'.$_GET['power_level'].'"';
        $query .= ($_GET['Voltage'] === "") ? '' :  ' and voltage = "'.$_GET['Voltage'].'"';
        $query .= ($_GET['Phase'] === "") ? '' :  ' and Phase= "'.$_GET['Phase'].'"';
        $query .= ($_GET['Modulate'] === "") ? '' :  ' and Modulate = "'.$_GET['Modulate'].'"';
        $query .= ($_GET['trap_type'] === "") ? '' :  ' and trap = "'.$_GET['trap_type'].'"';
        $query .= ($_GET['line_tuner_type'] === "") ? '' :  'and ltunner = "'.$_GET['line_tuner_type'].'"';
        echo $query;
}

提前感谢你们的帮助,我很感激。

4

3 回答 3

2

首先输入一个虚拟布尔表达式,以便所有其他条件都可以以“AND”开头。像这样:

        $query.= " where 1=1 ";
        if
        $query .= ($_GET['Transmission_Line_Designation'] === "") ? '' :  'AND Line_Designation = "'.$_GET['Transmission_Line_Designation'].'"';
        $query .= ($_GET['Switch_Number'] === "") ? '' :  ' and Switch_Number = "'.$_GET['Switch_Number'].'"';
        $query .= ($_GET['Telecom_Circuit_Number'] === "") ? '' :  ' and Telecom_Circuit_Number = "'.$_GET['Telecom_Circuit_Number'].'"';
        $query .= ($_GET['Transmitter_Frequency'] === "") ? '' :  ' and Transmitter_Frequency = "'.$_GET['Transmitter_Frequency'].'"';
        $query .= ($_GET['Receiver_Frequency'] === "") ? '' :  ' and Receiver_Frequency = "'.$_GET['Receiver_Frequency'].'"';
        $query .= ($_GET['power_level'] === "") ? '' :  ' and power = "'.$_GET['power_level'].'"';
        $query .= ($_GET['Voltage'] === "") ? '' :  ' and voltage = "'.$_GET['Voltage'].'"';
        $query .= ($_GET['Phase'] === "") ? '' :  ' and Phase= "'.$_GET['Phase'].'"';
        $query .= ($_GET['Modulate'] === "") ? '' :  ' and Modulate = "'.$_GET['Modulate'].'"';
        $query .= ($_GET['trap_type'] === "") ? '' :  ' and trap = "'.$_GET['trap_type'].'"';
        $query .= ($_GET['line_tuner_type'] === "") ? '' :  'and ltunner = "'.$_GET['line_tuner_type'].'"';
        echo $query;
}
于 2013-07-08T20:45:02.203 回答
1

if有什么问题?和循环?100%肯定它会比这段代码更好地维护......

尝试:

foreach($_GET as $key=>$value){
   if($value !== '')
      switch($key){
         case "Modulate":
            dosomething();
            break;
      }
   }

维护起来会好很多。

于 2013-07-08T20:49:52.050 回答
0

试试下面的,也添加了一些检查。

<?php

$sql   = "SELECT * FROM table WHERE ";
$count = count($_GET);

if ($count) { // check if $_GET has any values at all
    foreach ($_GET as $key => $value) {
        if ($value) { // check if $value not empty
            $sql .= $key . " = " . "'" . $value . "', ";
        }
    }

    $sql = rtrim($sql, ", ");
}

echo $sql;

?>
于 2013-07-08T21:02:49.933 回答