【发布时间】:2012-07-06 23:48:38
【问题描述】:
我想知道是否有人可以帮助我。
我正在使用下面的代码创建一个表,其中列出了location records 的current user。
<form name="locationsconsole" id="locationsconsole" method="post" action="locationsaction.php">
<table width="865">
<tr>
<th width="22"></th>
<th width="236"><div align="left">Location Name</div></th>
<th width="244"><div align="left">Location Address</div></th>
<th width="71"></th>
</tr>
<?php
$query = "SELECT l.*, COUNT(f.locationid) totalfinds FROM detectinglocations l LEFT JOIN finds f ON f.locationid = l.locationid WHERE l.userid = '$idnum' GROUP BY l.locationname";
$result = mysql_query($query) or die('error');
while($obj=mysql_fetch_object($result))
{
?>
<tr>
<td><input type="hidden" name="lid" value="<?php echo $obj->locationid;?>"/></td>
<td><?php echo $obj->locationname;?></td>
<td><?php echo $obj->returnedaddress;?></td>
<td><input name="type" type="submit" value="View Details"/></td>
</tr>
<?php
}
?>
</table>
</form>
您会看到每一行都有一个按钮,通过locationsaction.php 将用户带到另一个页面,如下所示。
<?php
session_start();
$_SESSION['lid'] = $_POST['lid'];
if (isset($_POST['type'])) {
$urls = array(
'View Details' => 'viewlocation.php'
);
$url = $urls[$_POST['type']];
header("Location: " . $url);
}
?>
表中当前有 3 条 location 记录是正确的,当单击按钮时,用户会被带到正确的屏幕。
但是,我遇到的问题是,无论我在哪一行单击按钮,检索到的记录始终与最后一个 location 记录相关。
我已经尝试对此进行排序已经有一段时间了,尽管重写了很多页面,但我一直无法找到解决方案。
我只是想知道是否有人可以看看这个,让我知道我哪里出错了。
【问题讨论】:
-
我不会提到这一点,只是你说你只是重写了这段代码......你应该真正解耦你的代码,即将你的数据访问例程与你的 html 输出分开。此外,
mysql_*函数基本上已被弃用,you should not be using them in new code。 -
嗨@joeshmo,谢谢你。我知道代码正在使用已弃用的函数。这是我最后一个使用 MySQL 的脚本。一旦这工作,我将更改我的所有脚本。但至少这将提供一个可靠的工作副本。亲切的问候
-
每当您在循环中提交表单时,盖子的最后一条记录总是在 $_POST['lid'] 中检索。我不认为你可以这样做。相反,你必须放置按钮,当它被点击时,你需要使用 javascript 来获取被点击的盖子的数据。
-
嗨@Kamal,感谢您抽出宝贵时间回复我的帖子。 '$lid' 被捕获在我的 'locationaction.php' 中。单击按钮时运行。我向所有人致以诚挚的歉意。我应该在我的原始帖子中包含这个。现在已添加。亲切的问候
标签: php html button html-table row