查询和 JOINS 的问题

Trouble with Queries and JOINS

我正在使用的主页称为项目详细信息页面,当您 select 表单上的项目编号时,将在子表单中查询与该项目编号相关的任何记录,并将它们显示在名为(任务子表单)。 这个名为 (TasksSubform) 的页面使用名为 mysqli_connect.php 的 php 文件来获取数据库连接,并将该连接分配给 mysqli_connect.php 文件中的 $dbc。 该页面然后查询名为'CommonTasks'的table 1,并开始在页面上使用

在table中逐行显示数据
while($row = $result->fetch_assoc())

当前显示的记录中的一列名为“AssignedTo”,它在员工 table 中生成唯一的 ID 号,而不是与 ID 号关联的员工姓名的文本值。因此,我需要能够使用 Fetch 列出 CommonTasks Table 中的记录,然后,当它尝试在 Common Tasks Table 的“AssignedTo”列中显示值时,它必须查找Employees table 中的 ID 等于 Common Tasks Table 中的相同值,并将分配给字段的数字值替换为 Employees table.[=14 中的文本值=]

COMMONTASKS
    EMPLOYEES
    * Add
    * AssignedTo
    * Attachments
    * Cost
    * CostInDays
    * Description
    * DueDate
    * EmployeeID
    * ID
    * PercentComplete
    * Priority
    * StartDate
    * SubmissionDate
    * Title * ID
* Address
* BusinessPhone
* City
* Company
* CountryRegion
* EmailAddress
* FaxNumber
* FirstName
* HomePhone
* JobTitle
* LastName
* MobilePhone
* Notes
* StateProvince
* WebPage
* ZIPPostal Code

这就是我的。然而,它所产生的只是 php 页面上分配给字段中的空白。

enter image description here 我是 php 和 mysql 的新手。 这可能是我忽略的简单事情。 然而,这几天我一直在排查各种方法,似乎无法弄清楚我做错了什么。

    <?php
// Get a connection for the database
require_once('../mysqli_connect.php');

// Create a query for the database


$sql = "SELECT * FROM `CommonTasks`"; 
$employee = "SELECT ID, LastName, LastName FROM Employees JOIN CommonTasks ON Employees.ID=CommonTasks.AssignedTo"; 
$emp = "SELECT LastName, FirstName FROM Employees JOIN CommonTasks WHERE Employees.ID = CommonTasks.AssignedTo LIMIT 1";
$emp1 = "SELECT id as LastName, FirstName FROM Employees WHERE ID = CommonTasks.AssignedTo LIMIT 1";
// Get a response from the database by sending the connection
// and the query
$result1 = @mysqli_query($dbc, $sql);
$result2 = @mysqli_query($dbc, $emp);
 $result = $dbc->query($sql);
$link = "commntasks-insertdata.php"
?> 
<!DOCTYPE html>
<html lang="en">
<head>
<title>Common Tasks-subform</title>
<meta name="viewport"charset="utf-8" content="width=device-width, initial-scale=1.0">

</head>
<body>
<?php

echo " <table border='1' #6a8fba>  
 <caption>SUBFORM - Common Tasks</caption>
    <tr>
    <th>Job Title</th>
    <th>Due Date</th>
    <th>Start Date</th>
    <th>Cost</th>
    <th>Priority</th>
    <th>Percent Complete</th>
    <th>Assigned To</th>
    <th>Description</th>
    </tr>";

       if ($result->num_rows > 0) {
     // output data of each row

     while($row = $result->fetch_assoc()) {

       echo "<tr>";
       echo "<td><a href=  $link  > $row[Title]  </a></td>";
       echo "<td>". $row['DueDate'] . "</td>";
       echo "<td>". $row['StartDate'] . "</td> " ;
       echo "<td>". $row['Cost'] . "</td>";
       echo "<td>". $row['Priority'] . "</td>";
       echo "<td>". $row['PercentComplete'] . "</td> " ;
       echo "<td>". $row ['SELECT LastName, FirstName FROM Employees JOIN CommonTasks WHERE Employees.ID = $_GET[AssignedTo] LIMIT 1'] . "</td>";
       echo "<td>". $row['Description'] . "</td> " ;
       echo "</tr>";
    }
}
echo "</table>";
?>
</body>

目前,该行正在生成结果

$result = $dbc->query($sql);

以下行将不会执行 mysql 查询。

echo "<td>". $row ['SELECT LastName, FirstName FROM Employees JOIN CommonTasks WHERE Employees.ID = $_GET[AssignedTo] LIMIT 1'] . "</td>";

正如所写,您正试图在 $result 中查找不存在的行值。您需要在第一个 while 循环中调用第二个查询并传递 $_GET[AssignedTo] 的值,它可能是 $row[AssignedTo]

像这样

while($row = $result->fetch_assoc()) {

   echo "<tr>";
   echo "<td><a href=  $link  > $row[Title]  </a></td>";
   echo "<td>". $row['DueDate'] . "</td>";
   echo "<td>". $row['StartDate'] . "</td> " ;
   echo "<td>". $row['Cost'] . "</td>";
   echo "<td>". $row['Priority'] . "</td>";
   echo "<td>". $row['PercentComplete'] . "</td> " ;

   $emp = "SELECT LastName, FirstName FROM Employees JOIN CommonTasks WHERE Employees.ID = '$row[AssignedTo]' LIMIT 1";
   $result2 = @mysqli_query($dbc, $emp);
   $row2 = $result2->fetch_assoc();


   echo "<td>". $row2 ['Firstname'] . " ". $row2 ['Lastname'] . "</td>";
   echo "<td>". $row['Description'] . "</td> " ;
   echo "</tr>";
}

所以,这是我对你的例子的修改:

while($row = $result->fetch_assoc()) {

   echo "<tr>";
   echo "<td><a href=  $link  > $row[Title]  </a></td>";
   echo "<td>". $row['DueDate'] . "</td>";
   echo "<td>". $row['StartDate'] . "</td> " ;
   echo "<td>". $row['Cost'] . "</td>";
   echo "<td>". $row['Priority'] . "</td>";
   echo "<td>". $row['PercentComplete'] . "</td> " ;

   $emp = "SELECT LastName, FirstName FROM Employees JOIN CommonTasks WHERE Employees.ID = '$row[AssignedTo]'";
   $result2 = @mysqli_query($dbc, $emp);
   $row2 = $result2->fetch_assoc();


   echo "<td>". $row2['LastName']," , ",$row2[FirstName] . "</td>";
   echo "<td>". $row['Description'] . "</td> " ;
   echo "</tr>";
}
}
echo "</table>";
?>

产生这个:Results of modified code

非常感谢您的帮助! 由于我正在将 Access 数据库转换为 mySQL 并重新创建所有查询、表单和报告....我相信在不久的将来我会有大量的问题。

埃里克