Skip to content
Advertisement

$_SESSION problem – I have same ID on all

I don’t know, how I do this: I want to add $id from database to $_SESSION["dbID"] and after click, it shows me more information from database. But table generates in while function and $_SESSION["dbID"] every time set to the highest number of row from table. Please, Can you anyone change my code as I have $_SESSION["dbID"] on every <tr> of table different? Thank you

while($row = $result->fetch_assoc())
{
   $id=$row['ID'];
   $name=$row['Name'];
   $subject=$row['Subject'];
   $date=$row['Date'];

   echo 
       '<tr class="trX" id="'.$id.'" href="google.com&id='.$row['ID'].'">
           <td class="tdX"><a style="color:black; text-decoration: none;" href="page.php">' . $id . '</a></td>
           <td class="tdX"><a style="color:black; text-decoration: none;" href="page.php">' . $name . '</td>
           <td class="tdX"><a style="color:black; text-decoration: none;" href="page.php">' . $subject . '</td>
           <td class="tdX"><a style="color:black; text-decoration: none;" href="page.php">' . $date . '</td>
       </tr>';
 
}
$_SESSION["dbID"] = $id;
echo ' </table>  ';

Advertisement

Answer

Explanation

You can do away with all of the a tags and use JavaScript to handle the redirect…

$url = "/path/to/file.php?id=" . $id;

Set the URL to the page that you want to link to. The line above shows the link to the file “file.php” on the server with the query string “id=$id”.

onclick="window.location.href='...'"

The line above is a JS equivalent of href. If you want to navigate to a server outside of your domain remember to add the full url e.g. https://www.website.com

Code example

while ($row = $result->fetch_assoc()) {
   $id      = $row['ID'];
   $name    = $row['Name'];
   $subject = $row['Subject'];
   $date    = $row['Date'];

   $url     = "/url/path.php?id=" . $id;

   echo <<<EOT
       <tr class="trX" onclick="window.location.href='{$url}'">
           <td class="tdX">{$id}</td>
           <td class="tdX">{$name}</td>
           <td class="tdX">{$subject}</td>
           <td class="tdX">{$date}</td>
       </tr>
EOT;
}
echo ' </table>  ';
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement