GY22 GY22 - 4 months ago 18
MySQL Question

Passing data from clickable table row to detail page using PHP

I am trying to make each row in my table clickable. So when a user clicks on a row it should go to a details page containing the data from that row.

Part of my html code:

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


My table looks like this:

enter image description here

When I click on a row it does redirect to the page, with the id in the url

the url for the user details page looks like this:

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

Code for the user detail page is:

<?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'];
}
?>


Then I click through to the details page, it show an error (internal server error, which means for me out of experience that my php code is incorrect).

So my question is how do i display all the data (name, email, role, etc) in the details page?

Answer

I figured out how to display the data, so on the user detail page I have the following code above the DOCTYPE html tag:

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

?>

Then in the body i read out the data like so:

<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>";

              }
            ?>