2017-07-06 50 views
-4

我无法使用数据库中的ID选择一组特定数据。例如,员工有一个唯一的ID为e000000001,当我点击索引中的查看按钮将导致员工详细信息页面显示该特定员工的详细信息,而不是所有员工的详细信息。谢谢。按ID选择数据PHP

//从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); 
?> 
+0

看起来你需要写'employeedetail.php'。这不是我们可以为您解答的问题。 – tadman

+1

你忘了描述这个问题。 “我有麻烦”并没有真正告诉我们什么是错的。 – David

+0

谢谢你的回复。我从数据库中检索的数据是全部员工。我不知道如何通过Id检索特定的一组数据。 – wen

回答

0

// FROM EMPLOYEE PAGE

您检索URL查询字符串是错误的方式。您应该使用$ _GET从URL获取查询字符串。在你的情况下,它应该是$ _GET ['id']。看到下面的代码:

require_once 'db/dbEmpDetail.php'; 
$employeeid = trim(mysqli_real_escape_string($_GET['id'])); 
$sql = "SELECT * FROM employees where empID = '".$employeeid."' "; 
$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); 
?> 
+0

您正在访问$ _GET而没有任何敏感性。 –

+0

@MuhammadUsman是的,我编辑了我的答案。 $ _GET应该总是逃脱/消毒。感谢提醒:D! –

+0

@muhammadUsman感谢您的帮助。我可以得到正确的细节。谢谢! – wen