使用链接到特定配置文件的列在PHP数据库中循环

问题描述:

I have a website where I am getting information of college student profiles on a database and displaying it as a linked collection. When I'm looping through the database I want to give each row a specific link to the profile of the student. Right now I am linking it to "profilePage.html" with generic information but I want it to be correlated with the row the user chose on the last(college) page.How do I save/transfer that information to the page. I do not want multiple profile pages but one template that is filled with the user previous choice.

    <?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 } ?>

Key thing, my urls are mysite.com/college.php and have no id's to specify them.

Structure of the Database student_info: Shows the structure of the database

First, do you have an URL Rewriting ? If not, your target page should be a PHP page, like profilePage.php.

Your link to this page have to include a parameter (Query String) which is, for example, the student's ID :

<a href="profilePage.php?id=<?php echo $row['id'] ?>" class="collection-item">

This type of URL will be generated: profilePage.php?id=36

In profilePHP.php, retrieve the parameter in the Query String :

<?php
$idStudent = mysql_real_escape_string($_GET['id']);
?>

mysql_real_escape_string() is really important, it prevents SQL injections.

After that, you could retrieve the student's informations with a SQL query.

<?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>

A little advice: mysql_query() will disappear soon, you should take a look at PDO.