我有一个网站,我可以在其中获取数据库中大学生文件的信息并将其显示为链接集合。当我循环访问数据库时,我想为每一行提供一个指向学生个人资料的特定链接。现在我将其链接到带有通用信息的“profilePage.html”,但我希望它与用户在最后(大学)页面上选择的行相关。如何将该信息保存/传输到页面。我不需要多个个人资料页面,而是需要一个包含用户先前选择的模板。
<?php
$result = mysql_query("SELECT * FROM student_info WHERE college='Boston College'", $db);
if (!$result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
?>
<a href="profilePage.html" class="collection-item">
<div class="row summary">
<div class="col s4 center">
<img class = "profile-pic" src="img/defaultProfile.jpg">
</div>
<div class="col s8">
<div class="title"> <?php echo $row[student_Name]; ?> </div>
<div class="black-text condensed thin"><i class="tiny material-icons">today</i> Founder, CEO at Linkle since January 1st, 2015</div>
<div></div>
</div>
</div>
</a>
<?php } ?>
关键是,我的网址是 mysite.com/college.php,并且没有 id 来指定它们。
数据库student_info的结构: Shows the structure of the database
最佳答案
首先,你们有 URL 重写吗?如果不是,您的目标页面应该是 PHP 页面,例如 profilePage.php。
您到此页面的链接必须包含一个参数(查询字符串),例如学生的 ID:
<a href="profilePage.php?id=<?php echo $row['id'] ?>" class="collection-item">
将生成此类 URL:profilePage.php?id=36
在profilePHP.php中,检索查询字符串中的参数:
<?php
$idStudent = mysql_real_escape_string($_GET['id']);
?>
mysql_real_escape_string()真的很重要,它可以防止SQL injections .
之后,您可以通过 SQL 查询检索学生的信息。
<?php
$idStudent = mysql_real_escape_string($_GET['id']);
$sql = sprintf("SELECT * FROM student_info WHERE id = %s", $idStudent);
$query = mysql_query($sql);
$student = mysql_fetch_object($query);
// you should check if the ID exists, and if there is 1 result
?>
<p>Student name is <?php echo $student['student_Name'] ?></p>
一点建议:mysql_query()很快就会消失,你应该看看PDO .
关于php - 使用链接到特定配置文件的列循环遍历 PHP 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34890771/