【发布时间】:2017-07-06 16:19:57
【问题描述】:
我无法使用数据库中的 ID 选择一组特定数据。例如,员工 1 的唯一 ID 为 e000000001,当我单击索引中的查看按钮时,将转到员工详细信息页面,该页面显示该特定员工的详细信息,而不是所有员工的详细信息。谢谢你。
//来自index.php页面
<?php
require_once 'db/dbEmpList.php';
$sqlStr = "SELECT * FROM employees;";
$result = $connection->query($sqlStr);
if ($result->num_rows > 0) {
echo "<table class='table table-sm'><thread><tr><th>Full Name</th><th>Employee ID</th><th>Position</th><th>View Employee's Details</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td>"
. $row["empName"]. "</td><td>"
. $row["empID"]. "</td><td>"
. $row["position"]. "</td>"
. "<td> <a href='employeedetail.php?id={$row["empID"]}'>View</a>"
. "</td></tr>";
}
}
//来自员工页面
require_once 'db/dbEmpDetail.php';
$sql = "SELECT * FROM employees where empID = '{$row["empID"]}' ";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>' .'<td>' .$row["empName"].'</td>'.'<td>'. $row["position"].'</td>' .'<td>'.$row["empNRIC"].'</td>' .'<td>'.$row["empID"].'</td>' .'<td>'.$row["empEmail"].'</td>' .'<td>'.$row["empPwd"].'</td>' . "</tr>";
}
} else {
echo "0 results";
}
mysqli_close($connection);
?>
【问题讨论】:
-
看来你需要写
employeedetail.php。这不是我们可以为您回答的问题。 -
您忘记描述有关问题的任何内容。 “我有麻烦”并不能真正告诉我们出了什么问题。
-
感谢您的回复。我从数据库中检索到的数据是所有员工。我不知道如何通过 Id 检索一组特定的数据。
-
@wen:所以您问的是如何在 PHP 中从数据库中获取特定数据?我相当肯定有教程和示例可以帮助解决这个问题。您要使用的 SQL 关键字称为:
WHERE。例如:SELECT * FROM SomeTable WHERE SomeColumn = ? -
@David 感谢您的回复。我厌倦了使用 where 但它不起作用。不能在 select 语句中声明 ID。这取决于用户在指向特定页面的索引页面上单击哪个视图按钮。