Jebil Jebil - 6 months ago 15
SQL Question

How to give a hyper link for user name

enter image description hereIn my admin page, admin can view the user id, user name and email id. Now, the admin can click user name, then it displays the details of that particular user name. I don't know how to do. Please help. My code is given below.

<html>
<head>
<title>Admin Page</title>
</head>
<body>
Welcome
<?php
include ('connect.php');
session_start();
$admin = $_SESSION['adminname'];
echo "$admin";?>
<b><a href="index.php"> SIGN OUT</a></b>
<table border="2" align="center">
<thead>
<tr>
<th>User_id</th>
<th>User Name</th>
<th>Email ID</th>
</tr>
</thead>
<tbody>
<?php
$query = mysqli_query($con, "SELECT * FROM sample");
while($row = mysqli_fetch_array($query))
{
$id=$row[0];
$name=$row[1];
$mail=$row[2];

echo
"<tr>
<td>{$row[0]}</td>
<td>{$row[1]}</td>
<td>{$row[2]}</td>";
}?>
</tbody>
</table>
</body>
</html>

Answer

First you query your database to get all username,ID and email. Then you do a loop to display it. Key is in your loop you can do like this.

<?php
    $query = mysqli_query($con, "SELECT * FROM sample");
    while($row = mysqli_fetch_array($query)){
         $id=$row[0];
         $name=$row[1];
         $mail=$row[2];

         echo "<tr>";
         echo "<td>";
         echo "<a href='?userid=$id'>$id</a>";
         echo "</td>"
         echo "<td>";
         echo "<a href='?userid=$id'>$name</a>";
         echo "</td>"
         echo "<td>";
         echo "<a href='?userid=$id'>$email</a>";
         echo "</td>"
         echo "</tr>";
  }
?>

This is result from loop above.

<tr>
    <td>
         <a href='detail.php?userid=1'>Fullname1 : Email1@email.com</a>
         <a href='detail.php?userid=2'>Fullname2 : Email2@email.com</a>
         ...
    </td>
</tr>

Now you will see this in your URL ?userid=1

You can do this on top of detail.php file $user_id = $_GET['userid'];

Then you get your user_id // 1 2 3 4 5 ... whatever

Now you do a query again with $user_id

like this :

$query = "SELECT * FROM sample WHERE user_id = $user_id";
while($row = mysqli_fetch_array($query)){
     //do what you want with this user data
}
Comments