dongsou8980 2016-01-10 06:59
浏览 94
已采纳

使用php mysqli显示人员列表中的详细信息

I am trying to display a list of persons. On clicking the list , more details on the person must be shown.

members.php: -a page to list all members

<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <h1>Our members are</h1>
    <?php 
    include 'config.php';
    session_start();
    $sql = "SELECT name from users";
    $result = $conn->query($sql);
    echo '<table>';
    if($result->num_rows > 0)
    {
        while($row = $result->fetch_assoc())   
                        {
                             echo '<tr>';
                             $_SESSION['selection'] = $row['name'];
                             echo '<td><a href = "view.php">'.$row['name'].'</a></td>'; 
                             echo '</tr>';
                        }
    }
    echo '</table>';
    $conn->close();
    ?>
</body>

view.php -the page to show more details:

<html>
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <?php
    include 'config.php';
    session_start();
    $pkey = $_SESSION['selection'];
    $new = "SELECT * FROM users where name = '$pkey'";
    $display = $conn->query($new);
    $result = $display->fetch_assoc();
    echo 'Name:'.$result['name'];
    echo 'Rollno:'.$result['rool_no'];
    echo 'Description:'.$result['description'];

    $conn->close();
    ?>
</body>

on using session , always the details of last person in the list is shown. How to display the corresonding details??

  • 写回答

1条回答 默认 最新

  • dqh19413 2016-01-10 07:18
    关注

    finally you are saving last name in the session variable $_SESSION['selection'] = $row['name']; so you getting the last name .while loop end with last name and you saving it in session.you are not using session as array know that.

    my advice is not use session. session is not required here.

    while($row = $result->fetch_assoc())   
                            {
                                 echo '<tr>';
                                 $_SESSION['selection'] = $row['name'];
                                 echo '<td><a href = "view.php">'.$row['name'].'</a></td>'; 
                                 echo '</tr>';
    
              }
    

    change these lines in members.php

     while($row = $result->fetch_assoc())   
                        {
                             echo '<tr>';
                             echo '<td><a href = "view.php?name=$row['name']">'.$row['name'].'</a></td>'; 
                             echo '</tr>';
                        }
    

    and change these line in view.php

    <body>
    <?php
    include 'config.php';
    session_start();
    $pkey = $_REQUEST['name'];//change this line alone
    $new = "SELECT * FROM users where name = '$pkey'";
    $display = $conn->query($new);
    $result = $display->fetch_assoc();
    echo 'Name:'.$result['name'];
    echo 'Rollno:'.$result['rool_no'];
    echo 'Description:'.$result['description'];
    
    $conn->close();
    ?>
    

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

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)