我创建了一个PHP表单,允许用户注册并登录。现在,我创建了另一个名为View.php的页面,该页面将显示我的MySQL
数据库中的所有注册用户。我使用的代码是
while($row=mysqli_fetch_assoc($sql))...
并且它成功地显示了所有用户。
现在我创建了另一个PHP页面,我将其命名为profile.PHP。我想从view.PHP上的每个结果中添加一个链接,该链接将重定向到profile。PHP?user=(他们的用户名)。但我不知道怎么做。
在行中:
echo "<small><a href = 'profile.php?user=$them'>[View Profile]</a></small><br/>";
不要使用固定的$they,只需使用$row['id']即可。然后,您可以在profile.php文件中获取具有该id的用户:
$id = $_GET['user'];
$sql = "SELECT * FROM users where id = $id";
请注意,此代码易于进行sql注入。我发布它只是为了让这个想法更容易理解。请看这里如何正确操作。
我不知道你用来实现结果的代码,但有一些类似的代码:
$query = "SELECT * FROM database WHERE id=$id";
$query = mysql_query($query);
这将根据用户id 过滤掉配置文件页面
在view.php中,考虑到您有一个名为"username"的列,请更改以下内容:请不要,最好放ID列。如果你想放ID列,只需将$row['username']
更改为$row['id']
,在profile.php
中的查询中也一样
<?php
...
while($row=mysqli_fetch_assoc($result)) {
echo "---------------------<br/>";
echo "<b>".$row['fullname']."</b><br/>";
echo "<small><i>".$row['course']."</i></small><br/>";
echo "<small><a href = 'friends.php?user=".$row['username']."'>[View Profile]</a></small><br/>";
echo "---------------------<br/><br/>";
}
?>
在你的profile.php
<?php session_start();
if($_SESSION['logged_in']==false) {
header("Location:login.php");
}
include("header.php");
?>
<html>
<head>
<title>View School-Mates</title>
</head>
<body>
<center>
<h1>My School-Mates</h1>
<small>View or Add them in your Trust List</small>
<br/><br/>
<hr>
</center>
<?php
try {
$dbh = new PDO('mysql:host=localhost;dbname=test_basic', "root", "");
$stmt = $dbh->prepare("SELECT * FROM USERS WHERE username= ?");
if ($stmt->execute(array($_GET['user']))) {
while ($row = $stmt->fetch()) {
//here you will have your row with all your username data
}
}
} catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
?>
</body>
</html>
请从这里阅读更多关于PDO的信息,以及如何进行连接这是必需的,因为你从$_get变量中获取数据,因此你需要避免sql注入
希望这是你想要的,如果不是,请告诉我,这样我就可以调整代码