我遇到了这个问题,我正在努力想出最好和最有效的解决方案。我有一个 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;
}
提前感谢你们的帮助,我很感激。