查询和连接有问题

w1e3prcc  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(349)

我使用的主页称为“项目详细信息”页,当您在窗体上选择项目编号时,该页将查询子窗体中与该项目编号相关的任何记录,并在名为(tasksubform)的页面中显示这些记录。这个名为(tasksubform)的页面使用一个名为mysqli\u connect.php的php文件来获取数据库连接,并将该连接分配给mysqli\u connect.php文件中的$dbc。然后,此页将查询名为“commontasks”的表1,并开始在页上的表中使用逐行显示数据

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

当前显示的记录中有一列名为“assignedto”,它在employees表中生成唯一的id号,而不是与id号关联的employees名称的文本值。因此,我需要能够使用fetch列出commontasks表中的记录,然后,当它试图显示commontasks表中“assignedto”列中的值时,它必须在employees表中查找与commontasks表中的值相同的id,并用employees表中的文本值替换assigned to字段的数字值。

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页面上assigned to字段中的一个空白。
在这里输入图片描述我是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>
qfe3c7zg

qfe3c7zg1#

下面是我对你例子的修改:

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>";
?>

产生这样的结果:修改代码的结果
非常感谢你的帮助!由于我正在将access数据库转换为mysql并重新创建所有查询、窗体和报表。。。。我相信在不久的将来我会有大量的问题。
埃里克

evrscar2

evrscar22#

目前,这条生产线正在生产结果

$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循环中调用第二个查询,并传递$\u 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>";
}

相关问题