在超链接中显示数据,并链接到另一个.php页面


Display data in hyperlinks and link to another .php page

我得到了这个程序,它在超链接中显示员工ID,用户可以单击其中一个,并将您带到另一个名为task7.php的php页面。此task7.php页面链接到一个HTML页面,用户在其中输入员工id并显示各种购买详细信息(这很好)。

似乎发生的事情是,一旦用户单击超链接,字段名显示在表中,但没有该人员id的详细信息。我认为我创建的程序被困在认为它只能得到一个员工id的详细信息,如果它被输入到task7.htm,而不是通过超链接。我想知道的是我如何运行查询,通过超链接显示所有购买细节,而不会损坏我创建的当前代码,通过用户输入显示细节。


注意:当我单击任何超链接的人员id时,我不仅得到表中的字段名称,而且还收到一个错误:

注意:未定义索引:staffID在I:'twa'twa291'task7.php.


下面是超链接代码:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Prac 2 Task 3</title>
</head>
<body>
<?php
$conn = mysql_connect("localhost", "twa291", ".....");
mysql_select_db("factory291", $conn)
or die ('Database not found ' . mysql_error() );
$sql = "SELECT staffID, staffName FROM staff";
$rs = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());

?>

<table border="1" summary="Staff Orders">
<tr>
<th>Staff ID</th>
<th>Staff Name</th>
</tr>
<?php
while ($row = mysql_fetch_array($rs)) { ?>
<tr>
<td><a href="task7.php?staffno=<?php echo $row["staffID"]?>">
    <?php echo $row["staffID"]?></a><
<td><?php echo $row["staffName"]?></td>
</tr>

<?php   }
mysql_close($conn); ?>
</table>
</body>
</html>

task7.php:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Prac 2 Task 3</title>
</head>
<body>
<?php
$conn = mysql_connect("localhost", "twa291", "......");
mysql_select_db("factory291", $conn)
or die ('Database not found ' . mysql_error() );  ?>
<?php
$staffid= $_GET["staffID"];
?>
<?php
$sql = "SELECT orderID, orderDate, orderDate, shippingDate, staffName FROM purchase, 
staff 
WHERE staff.staffID='$staffid'"; 

$rs = mysql_query($sql, $conn)
or die ('Problem with query' . mysql_error());
?>
<table border="1" summary="Staff Orders">
<tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Shipping Date</th>
<th>Staff Name</th>
</tr>
<?php
while ($row = mysql_fetch_array($rs)) { ?>
<tr>
<td><?php echo $row["orderID"]?></td>
<td><?php echo $row["orderDate"]?></td>
<td><?php echo $row["shippingDate"]?></td>
<td><?php echo $row["staffName"]?></td>
</tr>

<?php   }
mysql_close($conn); ?>
</table>
</body>
</html>

谢谢你的帮助!

您使用密钥staffno创建url,然后尝试检索密钥staffID

task7.php更改:

$staffid= $_GET["staffno"]; // not $staffid= $_GET["staffID"];

或更改超链接代码:

<td><a href="task7.php?staffID=<?php echo $row["staffID"]?>">

在第一页你传递"staffno"参数但在task7.php你得到$_GET["staffID"];

所以这样修改:

<a href="task7.php?staffID=<?php echo $row["staffID"]?>">

我个人更喜欢使用"staffID"而不是"staffno",所以它与DB中的列名相同