douzhun5971 2017-04-26 11:15
浏览 173

PHP从循环中获取每个对象的id并使用另一个页面

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>";

?>
  • 写回答

1条回答 默认 最新

  • duanbaque4230 2017-04-26 11:19
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大