根据下拉选择调整查询

时间:2014-09-04 16:08:05

标签: php mysql drop-down-menu

我想要得到的是:

我的php文件中有以下变量:

$tech= $_GET['Combobox10'];
$shopfloor = $_GET['Combobox8'];

我想要做的是,如果从HTML表单中将变量$ tech(这是一个下拉列表)选为“all”,则必须运行以下查询才能显示表中的所有Tech - 就好像有部分与sql查询的Tech相关的部分不存在。 $ shopfloor也一样 - 如果选中的下拉列表是'all',我希望查询忽略$ shopfloor的部分并显示全部。当然,如果选择了特定的车间或技术,则必须强制执行查询的部分内容。

$result = mysql_query ("SELECT * FROM dbo_shopfloorcells INNER JOIN (dbo_noted_by INNER JOIN (dbo_leader_name INNER JOIN (dbo_Station INNER JOIN (dbo_Shop_Floor INNER JOIN (dbo_Fault_Desc INNER JOIN 
(dbo_Machine INNER JOIN (dbo_Line_Cell_Binding INNER JOIN (dbo_Tech_Name INNER JOIN (dbo_Maint_Category INNER JOIN dbo_newprevlog2 ON dbo_Maint_Category.ID = dbo_newprevlog2.ID_Maint_Cat) 
ON dbo_Tech_Name.ID = dbo_newprevlog2.ID_Tech_Name) ON dbo_Line_Cell_Binding.PKID = dbo_newprevlog2.ID_Line_Desc) ON dbo_Machine.PKID = dbo_newprevlog2.ID_Mach_Desc) 
ON dbo_Fault_Desc.ID = dbo_newprevlog2.ID_Fault_Desc) ON dbo_Shop_Floor.ID = dbo_newprevlog2.ID_Shop_Floor) ON dbo_Station.PKID = dbo_newprevlog2.ID_Station_No) 
ON dbo_leader_name.ID = dbo_newprevlog2.ID_Leader_Name) ON dbo_noted_by.ID = dbo_newprevlog2.ID_Noted_By) ON (dbo_shopfloorcells.PKID = dbo_Line_Cell_Binding.ID_Cell_Shop_Floor) 
AND (dbo_shopfloorcells.ID_Shop_Floor = dbo_Shop_Floor.ID)
GROUP BY dbo_newprevlog2.ID, dbo_newprevlog2.Status, dbo_Shop_Floor.Shopfloor, dbo_shopfloorcells.Cell, dbo_Line_Cell_Binding.Line_Name, 
dbo_Machine.Machine_Section, dbo_Station.Station_, dbo_Tech_Name.Technician_Name, dbo_Maint_Category.Maintenance_Category, dbo_Fault_Desc.Fault_Description, 
dbo_newprevlog2.Intervention, dbo_newprevlog2.Remarks, dbo_newprevlog2.ComponentsParts, dbo_newprevlog2.StartDate1, dbo_newprevlog2.EndDate1, dbo_newprevlog2.StartTime1, 
dbo_newprevlog2.EndTime1, dbo_newprevlog2.DurationTime, dbo_newprevlog2.ID_Shop_Floor, dbo_Line_Cell_Binding.ID_Cell_Shop_Floor
having (dbo_newprevlog2.ID_Shop_Floor = $shopfloor) AND ((dbo_newprevlog2.ID_Tech_Name = $tech));");

我不知道如何解决这个问题。我已经尝试将$ tech更改为此(感谢来自此论坛的用户@Déjàvu):

$tech = ($_GET['Combobox10'] != "all") ? $_GET['Combobox10'] : '';

但是这次没有给我所需的结果。

1 个答案:

答案 0 :(得分:0)

方法1

尝试以下

$tech = ($_GET['Combobox10'] != "all") ? $_GET['Combobox10'] : "dbo_newprevlog2.ID_Tech_Name";

$shopfloor = ($_GET['Combobox8'] != "all") ? $_GET['Combobox8'] : "dbo_newprevlog2.ID_Shop_Floor";

$tech = ($_GET['Combobox10'] != "all") ? $_GET['Combobox10'] : true;

$shopfloor = ($_GET['Combobox8'] != "all") ? $_GET['Combobox8'] : true;

我们的想法是跳过having最后一行的检查(mysql_query),并使它们始终为真。


方法2(推荐)

虽然更好的解决方案可能会完全改变你的mysql_query,但不会跳过这些检查。

更改您的SQL查询(根据您的需要在评论后修改)

请注意,$ check部分实际上决定应用哪个SQL查询参数,具体取决于是否选择了一个或两个组合框“全部”。

$check = "";
    if ($tech != "all" or $shopfloor != "all") $check .= "having ";
    if ($tech != "all") $check .= "(dbo_newprevlog2.ID_Tech_Name = ".$tech.")";
    if ($tech != "all" and $shopfloor != "all") $check .= " AND ";
    if ($shopfloor != "all") $check .= "(dbo_newprevlog2.ID_Shop_Floor = ".$shopfloor.")";

    $result = mysql_query ("SELECT * FROM dbo_shopfloorcells INNER JOIN (dbo_noted_by INNER JOIN (dbo_leader_name INNER JOIN (dbo_Station INNER JOIN (dbo_Shop_Floor INNER JOIN (dbo_Fault_Desc INNER JOIN 
    (dbo_Machine INNER JOIN (dbo_Line_Cell_Binding INNER JOIN (dbo_Tech_Name INNER JOIN (dbo_Maint_Category INNER JOIN dbo_newprevlog2 ON dbo_Maint_Category.ID = dbo_newprevlog2.ID_Maint_Cat) 
    ON dbo_Tech_Name.ID = dbo_newprevlog2.ID_Tech_Name) ON dbo_Line_Cell_Binding.PKID = dbo_newprevlog2.ID_Line_Desc) ON dbo_Machine.PKID = dbo_newprevlog2.ID_Mach_Desc) 
    ON dbo_Fault_Desc.ID = dbo_newprevlog2.ID_Fault_Desc) ON dbo_Shop_Floor.ID = dbo_newprevlog2.ID_Shop_Floor) ON dbo_Station.PKID = dbo_newprevlog2.ID_Station_No) 
    ON dbo_leader_name.ID = dbo_newprevlog2.ID_Leader_Name) ON dbo_noted_by.ID = dbo_newprevlog2.ID_Noted_By) ON (dbo_shopfloorcells.PKID = dbo_Line_Cell_Binding.ID_Cell_Shop_Floor) 
    AND (dbo_shopfloorcells.ID_Shop_Floor = dbo_Shop_Floor.ID)
    GROUP BY dbo_newprevlog2.ID, dbo_newprevlog2.Status, dbo_Shop_Floor.Shopfloor, dbo_shopfloorcells.Cell, dbo_Line_Cell_Binding.Line_Name, 
    dbo_Machine.Machine_Section, dbo_Station.Station_, dbo_Tech_Name.Technician_Name, dbo_Maint_Category.Maintenance_Category, dbo_Fault_Desc.Fault_Description, 
    dbo_newprevlog2.Intervention, dbo_newprevlog2.Remarks, dbo_newprevlog2.ComponentsParts, dbo_newprevlog2.StartDate1, dbo_newprevlog2.EndDate1, dbo_newprevlog2.StartTime1, 
    dbo_newprevlog2.EndTime1, dbo_newprevlog2.DurationTime, dbo_newprevlog2.ID_Shop_Floor, dbo_Line_Cell_Binding.ID_Cell_Shop_Floor".$check.";");

无需更改
$tech= $_GET['Combobox10'];
$shopfloor = $_GET['Combobox8'];