这是我的代码,我想要做的是在另一个页面中显示我的朋友的名字作为超链接,当点击链接时,我想显示数据。
<?php
$con=mysqli_connect("localhost","root","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM friends");
while($row = mysqli_fetch_array($result))
{
echo "<p>" . $row['FirstName'] . "</p>";
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
}
mysqli_close($con);
?>
答案 0 :(得分:0)
<?php
$con=mysqli_connect("localhost","root","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM friends");
while($row = mysqli_fetch_array($result))
{?>php
<a href=<?php "echo "<p>" . $row['FirstName'] . "</p>";"?>> `your page name`</a><?php
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
}
mysqli_close($con);
?>
答案 1 :(得分:0)
您的问题文件发生了变化:
while($row = mysqli_fetch_array($result))
{
echo "<p>" . $row['FirstName'] . "</p>";
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
}
到
while($row = mysqli_fetch_array($result))
{
echo '<a href="show.php?UserId='.$row['UserId'].'"> . $row['FirstName'] . "</a>";
}
show.php
$con=mysqli_connect("localhost","root","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_GET['UserId'])){
if(is_numeric($_GET['UserId'])){
$UserId = (int)$_GET['UserId'];
$result = mysqli_query($con,"SELECT * FROM friends WHERE UserId= $UserId");
if(count($result)){
while($row = mysqli_fetch_array($result))
{
echo "<p>" . $row['FirstName'] . "</p>";
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
}}}}
重要提示:我不确定什么是UserId列名,因此请进行更改。
另外,请检查语法错误。
答案 2 :(得分:0)
尝试使用数据库中的增量ID连接它。因此,您朋友表中的每一行都必须具有唯一的ID。通过这种方式,您可以连接它并从您的朋友表中获取行数据。
所以你的朋友表必须如下所示:
friend_id (unique id)
firstname
lastname
email
address
您的代码可能类似(这不是任何攻击的保存):
<?php
$con = mysqli_connect("localhost","root","abc123","my_db");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_GET['friendid'])) {
$result = mysqli_query($con,"SELECT * FROM friends");
while($row = mysqli_fetch_array($result)) {
echo "<p>" . $row['FirstName'] . "</p>";
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
echo "<p><a href=\"_YOUR_SCRIPT_URL_HERE_?friendid=".$row['friend_id']."\" target=\"_self\">Details</a></p>";
}
} else {
$result = mysqli_query($con,"SELECT * FROM friends WHERE friend_id = '".$_GET['friendid']."'");
$row = mysqli_fetch_assoc($result);
echo "<p>" . $row['FirstName'] . "</p>";
echo "<p>" . $row['LastName'] . "</p>";
echo "<p>" . $row['Email'] . "</p>";
echo "<p>" . $row['Address'] . "</p>";
}
mysqli_close($con);
?>