显示数据库中的数据并将其链接到包含所有配置文件详细信息的新页

时间:2018-05-07 21:31:50

标签: php mysql

我试图生成一个页面,其中包含我数据库中所有配置文件的名字和姓氏。应使用链接打印出名字。该链接应该能够被点击并链接到包含所有个人信息的新页面。

shownames.php:

<!doctype html>
<?php
include 'db.php';
$query = 'SELECT * FROM subjects';
$result = $conn->query($query);


if ($result->num_rows > 0) {
    echo "<table><th>Name</th><th></th><th>Moniker</th></tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td><a href = 'getprofile.php'>".$row["firstName"]." ".$row["lastName"]."</a></td><td>".$row["age"]."</td><td>".$row["moniker"]. "</td></tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}
$conn->close();
?>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
</body>
</html>

getprofile.php:

<?php
include 'db.php';
    $pkey = mysql_real_escape_string($_GET['firstname']);
    $query = "SELECT * FROM subjects WHERE firstName =" . $pkey;
    $result = $conn->query($query);


if ($result->num_rows > 0) {
    echo "<table><tr><th>ID</th><th>Name</th><th>AGE</th><th>Tattoo</th><th>Moniker</th></tr>";
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>".$row["id"]."</td><td>".$row["firstName"]." ".$row["lastName"]."</td><td>".$row["age"]."</td><td>".$row["tats"]."</td><td>".$row["moniker"]. "</td></tr>";
    }
    echo "</table>";
} else {
    echo "0 results";
}
?>

1 个答案:

答案 0 :(得分:0)

您要打印的锚元素需要包含查询参数。

echo "<tr><td><a href = 'getprofile.php?firstname=" . $row["firstName"]. "'>".$row["firstName"]." ".$row["lastName"]."</a></td><td>".$row["age"]."</td><td>".$row["moniker"]. "</td></tr>";