我有一个网站,可在该网站上获取数据库中大学生资料的信息,并将其显示为链接的收藏集。当我遍历数据库时,我想给每一行一个指向学生资料的特定链接。现在,我将其与具有常规信息的“ 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中,在Query String中检索参数:
<?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/