我有一个从数据库检索数据的代码。我需要根据对象的id在另一个页面上显示被点击对象的详细信息。我如何才能做到这一点?当我设置$_SESSION并在另一个页面中使用时,它只需要最后一个值。代码检索信息如下所示。
<?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>";
?>发布于 2017-04-26 19:19:43
您需要更改此行:
echo "<td ><a href='ad_details.php'>$training_name</a></td>";至:
echo "<td ><a href='ad_details.php?id=$id'>$training_name</a></td>";然后在details.php上使用get方法获取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
}旁注:如果您没有使用预准备语句,我强烈建议您开始使用它们。
https://stackoverflow.com/questions/43632629
复制相似问题