duangong1979 2016-01-20 02:57
浏览 41
已采纳

使用链接到特定配置文件的列在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

  • 写回答

1条回答 默认 最新

  • dongren4099 2016-01-20 10:27
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题