简体   繁体   English

使用PHP和来自多个表(包括“ id”参考表)的多个HTML下拉框查询MySQL结果

[英]Query MySQL results using PHP and multiple HTML dropdown boxes from multiple tables including 'id' reference tables

Tables structure: 表结构:

TESTLINKS 测试链接

  • id ID
  • source 资源
  • projectid 项目编号
  • clientid 客户编号
  • qa QA
  • stg g
  • prod 产品
  • username 用户名
  • password 密码

PROJECTS 工程项目

  • id ID
  • projectname 项目名
  • clientid 客户编号
  • projectdescription 项目描述
  • projectmanager 专案经理
  • qbid qbid
  • aetid 乙肝
  • uatdate at
  • targetreleasedate targetreleasedate
  • status 状态

PRODUCTS 产品

  • id ID
  • productname 产品名称
  • projectmanageruserid 项目经理

TESTLINKSPRODUCTS 测试链接产品

  • id ID
  • testlinksid 测试链接
  • productsid 产品编号

CLIENTS 客户

  • id ID
  • clientname 客户名称
  • clientmanager 客户经理

My goal is to create a system to input testing links and to enable the user to pull the appropriate testing links based on multiple options which they can select in a series of dropdown boxes. 我的目标是创建一个系统来输入测试链接,并使用户能够基于他们可以在一系列下拉框中选择的多个选项来拉出适当的测试链接。

I have setup the PHP and HTML pages to input data into the tables listed above. 我已经设置了PHP和HTML页面,以将数据输入到上面列出的表中。 Now I am trying to setup the MySQL query and PHP required to pull the data into the webpage. 现在,我正在尝试设置将数据提取到网页所需的MySQL查询和PHP。

I would like the user to be able to query the results based on 1) projects.id 2) testlinks.source 3) products.id 4) clients.id 我希望用户能够基于1)projects.id 2)testlinks.source 3)products.id 4)client.id来查询结果

these would be selected from 4 separate dropdown boxes 这些可以从4个单独的下拉框中选择

I was easily able to successfully pull results based on projects.id and testlinks.source because these two tables have 'projectid' in common. 我很容易能够成功地基于projects.id和testlinks.source提取结果,因为这两个表具有共同的“ projectid”。 However I am now having an issue pulling results based on 'productid' because the products are associated to the testlinks and not to the projects. 但是我现在在基于“ productid”获取结果时遇到了一个问题,因为产品与测试链接关联,而不与项目关联。 The 'testlinksproducts' table handles the association of products to testlinks but I do not know how to add this to the query because when I run the query I get rows repeated several times and doesn't achieve the results that I want. “ testlinksproducts”表处理产品与testlinks的关联,但是我不知道如何将其添加到查询中,因为当我运行查询时,我得到了重复多次的行,但没有达到我想要的结果。

Here is my PHP file which is working for pulling testlinks based on project.id and testlinks.source -- 这是我的PHP文件,用于根据project.id和testlinks.source提取测试链接-

$query = "SELECT testlinks.id,
                 testlinks.source,
                 testlinks.projectid,
                 testlinks.clientid,
                 testlinks.qa,
                 testlinks.stg, 
                 testlinks.prod, 
                 testlinks.username,
                 testlinks.password, 
                 projects.id, 
                 projects.projectname 
          FROM testlinks , projects 
          WHERE testlinks.projectid = projects.id 
            AND (testlinks.projectid='$projectid' 
                 OR testlinks.source='$source')
          ;"; 

$result = mysql_query($query) or die(mysql_error()); 

echo "<table class=\"zebra\">"; 
echo "<thead><tr>
    <th>ID</th>
    <th>Source</th>
    <th>Project Name</th>
    <th>QA</th>
    <th>STG</th>
    <th>Prod</th>
    </tr></thead>";
while($row = mysql_fetch_array($result))
{
    echo "<tr>"; 
    echo "<td>".$row['id']."</td>
        <td>".$row['source']."</td>
        <td>".$row['projectname']."  </td>
        <td>"."<a href=\"".$row['qa']."\" target=\"new\">
            <button>QA</button></a></td>
        <td>"."<a href=\"".$row['stg']."\" target=\"new\">
            <button>STG</button></a></td>
        <td>"." <a href=\"".$row['prod']."\" target=\"new\">
            <button>PROD</button></a></td>
        </tr>";
}
echo "</table>";

Can someone help with adding query functionality to pull testlinks based on productsid (contained in 'testlinksproducts' table) ? 有人可以帮助添加查询功能来基于productid(包含在“ testlinksproducts”表中)提取测试链接吗?

Help would be very much appreciated on this one! 帮助将不胜感激这一件事!

The query could be something like: 该查询可能类似于:

SELECT testlinks.id,
                 testlinks.source,
                 testlinks.projectid,
                 testlinks.clientid,
                 testlinks.qa,
                 testlinks.stg,
                 testlinks.prod,
                 testlinks.username,
                 testlinks.password,
                 projects.id,
                 projects.projectname
          FROM testlinks , projects , TESTLINKSPRODUCTS
          WHERE testlinks.projectid = projects.id
            AND testlinks.id = TESTLINKSPRODUCTS.testlinksid
            AND (testlinks.projectid='$projectid'
                 OR testlinks.source='$source'
                 OR TESTLINKSPRODUCTS.productsid = '$productid')
          GROUP BY testlinks.id

Note: The group-by may not be necessary if a testlink can only be in one project, product and client. 注意:如果一个testlink只能在一个项目,产品和客户中,则不需要分组依据。 A group_concat() might help if a testlink is in more than one project. 如果一个测试链接在多个项目中,则group_concat()可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM