I have a code that retrieves data from database. I need to show detailed information of clicked object on another page according to the id of the object. How can I achieve this? When I set $_SESSION and use in another page it takes only last value. Codes retrieves info is as following.
<?php
include("misc.inc");
session_start();
$connect = mysqli_connect($host,$user,$password,$database) or die ("Couldn't connect database");
$sql = "SELECT * FROM ad ORDER BY training_start desc";
$result = mysqli_query($connect,$sql) or die("Couldn't execute query.");
echo "<table cols='4' cellspacing='25'>";
echo "<th>ID</th>";
echo "<th>Training Name</th>";
echo "<th>Trainer Name</th>";
echo "<th>Training Dates</th>";
echo "<th>Registration Starts</th>";
while($row=mysqli_fetch_assoc($result))
{
extract($row );
echo "<tr><td >$id</td>";
echo "<td ><a href='ad_details.php'>$training_name</a></td>";
echo "<td >$trainer_name</td>";
echo "<td >$training_start - $training_end</td>";
echo "<td >$reg_start</td></tr>";
}
echo "</table>";
?>
You need to change this line :
echo "<td ><a href='ad_details.php'>$training_name</a></td>";
To :
echo "<td ><a href='ad_details.php?id=$id'>$training_name</a></td>";
Then on details.php use get method to get the id
if(isset($_GET['id']) && !empty($_GET['id'])){
$currentID = $_GET['id'];
// Then select what you want where id = $currentID
}else{
//id not set redirect back / return error
}
Side Note : if you are not using prepared statements I strongly advice that you start using them.