Mysql+php, how to make a link that shows all data from a record when you press the "headline"

18.6k views Asked by At

As the title says, and this is not probably a question, more a hint how to do it.

I have table with four fields, id (auto+primary), firstname, lastname, age.

On my index page I select *, then I make the firstname to a link which goes to antoher page which I want to show all data for that record.

For the moment I have to do it manually "select * from " where id="2". (the other page)

How do I make the link to autodetect "the id from that record set" and only display data from that record.

You can see my curreny project here,

http://www.adamskymotorkylare.se/business/

as you can see, when you click the "firstname" it will always display data where id=2 ( paris hilton ), what I want it to do, it when I click "jack" it will select * from where id="1"

Thanks in advance, Jack

"index page"

$result = mysqli_query($con,"SELECT * FROM persons");

echo "<table width=100%>
<tr>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Gender</th>
</tr>";

while($row = mysqli_fetch_array($result))
{

$id = $row['id'];

echo "<tr>";
echo "<td>" . $row['id'] .  "</td>";
echo "<td> <a href='view_more.php?id= . $id .'>" . $row['firstname'] . "</a> </td>";
echo "<td>" . $row['lastname'] .  "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "</tr>";
}
echo "</table>";

"viewmore page"

$id = $_get['id'];

$result = mysqli_query($con,"SELECT * FROM persons 
WHERE id='$id'");

echo "<table width=100%>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Gender</th>
</tr>";

while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <a href='#'>" . $row['firstname'] . "</a> </td>";
echo "<td>" . $row['lastname'] .  "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['gender'] . "</td>";
echo "</tr>";
}
echo "</table>";
3

There are 3 answers

9
DanDotNet On BEST ANSWER

Try this, on your page that indexes the users:

while($row = mysqli_fetch_array($result))
{

$id = $row['id'];
$firstName = $row['firstName'];

echo ('<a href="user_account.php?id=' . $id . '">' . $firstName . '</a>');
}

This means if someone clicks the first name they will be sent to user_account.php (can replace this obviously) and you will pass in the ID via the URL (user_account.php?id=123).

On the User Account page you want to do the following:

// GET ID FROM THE URL

$id = $_GET['id'];

$result = mysqli_query($con,"SELECT (WHATEVER YOU WANT) FROM (YOUR TABLE) WHERE id = $id");

Notes: Replace variables and query with the details you need.

I hope that goes some way to helping.

4
m-farhan On
<a href="./view_more.php?id=$id">UserFirstName</a>

$id is user record id first column of your table so when you send it to view_more.php page you can get clicked id $_GET['id']; and get user records for that id and link is user name

and $id you get form data base the way you get first name

1
Ayaz Shah On

index.php

<?php 
$sql =mysql_query("select * from `table`");
    while($row = mysql_fetch_array($sql)){
?>

    <?php $id = $row['id']; ?>

<a href="detail.php?cid=<?php echo $id; ?>"><?php echo ucwords($row['Firstname']); ?></a>

<?php 
    }
?>

on index.php anchor tag will send the user id to detail.php

detail.php

<?php
$id = $_REQUEST['cid'];
$sql =mysql_query("select * from `table` where id='".$id."'");
    while($row = mysql_fetch_array($sql)){
?>
    <tr>
        <th>First Name:</th>
        <th>Last Name:</th>
        <th>Age Name:</th>
        <th>Gender:</th>
    </tr>
    <tr>
        <td><?=$row['firstname']?></td>
        <td><?=$row['lastname']?></td>
        <td><?=$row['age']?></td>
        <td><?=$row['gender']?></td>
    </tr>
<?php 
    }
?>

on detail.php $id get the user id from index.php. After that id helps in query and while loop which is get and show the user data from database.