1

I'm showing a data table from MySQL, let say in x.php like

    <?php $sql = "SELECT * FROM tblname";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
     while($row = $result->fetch_assoc()) {

      $id=$row["id"]; 
         echo "<tr><td>". $row["0"]. "</td><td>". $row["1"]."</td><td>".$row["2"]."</td><td>".$row["3"]."</td><td>"."<a href='y.php?=$id'>More details</a>"."</td></tr>";
        $_SESSION["id"]=$id;

     }
} else {
}?>

But where I go to y.php class to see more details it shows data only from last row of MySQL records. How can I fix it?

arber
  • 113
  • 4
  • I only see one `$row`. Do you have a loop? How about a query? – Machavity Aug 20 '15 at 19:43
  • `select ... order by something desc limit 1`, then you only ever display the one row. – Marc B Aug 20 '15 at 19:44
  • http://php.net/manual/en/function.mysql-data-seek.php, to return to the start of the data SET mysql_data_seek – volkinc Aug 20 '15 at 19:45
  • @volkinc Those functions [are deprecated](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Machavity Aug 20 '15 at 19:47
  • show us the code you are using. From 2 lines of HTML it's difficult to offer you proper solution – volkinc Aug 20 '15 at 19:48
  • 1
    You're replacing the session variable each time through the loop. When the loop is done, it will contain the ID from the last row. – Barmar Aug 20 '15 at 19:54
  • @Barmar Upps,yes you're right thanks.But can you tell how should I do it? – arber Aug 20 '15 at 19:55

1 Answers1

2

What's wrong is you are using the $_SESSION to fetch the detail. What you want isn't to store the id in the $_SESSION, you want to put it in each link, so that when a user clicks a link he has access to the row detail.

Basically, parameters passed in a link (like index.php?param1=value1) are found in $_GET global variable.

So I guess in y.php you use $_SESSION['id'] to fetch the row detail, use $_GET['id'] instead.

Also, in x.php edit this :

"<a href='y.php?=$id'>More details</a>"

to :

"<a href='y.php?id=$id'>More details</a>"

And remove : $_SESSION['id'] = $id;

Loïc
  • 11,804
  • 1
  • 31
  • 49