GY22 GY22 - 1 year ago 93
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:

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

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">
<table id="myTable" class="table table-striped">
<th>Profile Picture</th>
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>";

$("#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;

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:


Code for the user detail page is:

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 Source

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

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>

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download