使用PHP将数据从可点击表格行传递到详细信息页面

时间:2016-07-29 16:09:38

标签: php mysql

我正在尝试让我的表中的每一行都可以点击。因此,当用户点击一行时,它应该转到包含该行数据的详细信息页面。

我的部分HTML代码:

<?php
ini_set('display_errors', 'On');   
error_reporting(E_ALL | E_STRICT);  

session_start();
include 'config.php';

$query_string = "SELECT * FROM tbl_User INNER JOIN tbl_Role ON tbl_User.role_id=tbl_Role.role_id";

$query = mysqli_query($con, $query_string);


?><!DOCTYPE html>
<html lang="en">
<head>
   ....
</head>
<body>
<table id="myTable" class="table table-striped">
          <thead>
            <tr>
              <th>Name</th>
              <th>Email</th>
              <th>Profile Picture</th>
              <th>Role</th>
            </tr>
          </thead>
          <tbody>
            <?php
              while($row = mysqli_fetch_assoc($query)){
                echo "<tr>";
                  echo "<td><a href=\"user_detail.php?user_id={$row['user_id']}\">{$row['name']}</a></td>\n"
                  echo "<td>".$row['email']."</td>";
                  echo "<td><img class='picTable' src='plugins/images/".$row['profile_picture']."'></td>";
                  echo "<td>".$row['role']."</td>";
                echo "</tr>";  
              }    
            ?>
          </tbody>
        </table>
   </body>

<script>
$("#myTable").on("click", "tr", function(e){
      // get the value
    var value = $(this).find('td:first').text();
    // redirect the user with the value as a GET variable
    window.location = 'user_detail.php?name=' + value;
    });
</script>
</body>

我的表格如下:

enter image description here

当我点击一行时,它会重定向到该页面,其中的ID位于网址

用户详细信息页面的网址如下所示:

http://localhost:8888/challenger/user_detail.php?user_id=5

用户详细信息页面的代码是:

<?php
session_start();
include_once 'config.php';

$qry = "SELECT * FROM tbl_User WHERE user_id = {$_GET['user_id'] "
$query = mysqli_query($con, $qry);

              while($row = mysqli_fetch_array($query)){
                echo $row['email'];
                echo $row.['name'];
              }
?>

然后我点击进入详情页面,它显示一个错误(内部服务器错误,这对我来说意味着我的PHP代码不正确)。

所以我的问题是如何在详细信息页面中显示所有数据(姓名,电子邮件,角色等)?

1 个答案:

答案 0 :(得分:0)

我想出了如何显示数据,因此在用户详细信息页面上,我在DOCTYPE html标记上方有以下代码:

<?php
session_start();
include_once 'config.php';

$id = $_GET['user_id'];
$query_string = "SELECT * FROM tbl_User INNER JOIN tbl_Role ON tbl_User.role_id=tbl_Role.role_id WHERE user_id = '$id'";

$query = mysqli_query($con, $query_string);

?>

然后在身体中我读出了这样的数据:

<h3 class="box-title">User Details</h3>

            <?php
              while($row = mysqli_fetch_assoc($query)){
                  echo "<h1>".$row['name']. "</h1>";
                  echo "<h1>".$row['email']. "</h1>";
                  echo "<h1>".$row['role']. "</h1>";
                  echo "<div><img class='picTable' src='plugins/images/".$row['profile_picture']."'></div>";

              }
            ?>