【问题标题】:Add button to table row which is linked to the data on that row将按钮添加到链接到该行数据的表行
【发布时间】:2012-07-06 23:48:38
【问题描述】:

我想知道是否有人可以帮助我。

我正在使用下面的代码创建一个表,其中列出了location recordscurrent 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


【解决方案1】:

在互联网上花了相当长的时间研究这个问题后,我找到了解决方案here。然后我可以编辑它以满足我对以下脚本的需要:

<?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');
                    $row = mysql_fetch_assoc($result);              
                    extract($row); 

                    /* Display Users in a table */ 
                    echo "<table cellspacing='5' border='0' cellpadding='0' width=95%>"; 
                    echo "<tr><td colspan='0' style='text-align: Left'></td></tr>\n"; 
                    echo "<tr><td style='font-weight: bold; 
                        font-size: 1.0em'>Location</td><td style='font-weight: bold; 
                        font-size: 1.0em'>Address</td><td style='font-weight: bold;  
                        font-size: 1.0em'>Finds Made</td></tr>\n";
                    mysql_data_seek($result, 0);

                    while($row = mysql_fetch_array($result))
                    {


                    /* display row for each user */ 
                        echo "<tr>\n"; 
                        $theID = $row['locationid']; 
                        echo " <td>{$row['locationname']}</td>\n"; 
                        echo " <td>{$row['returnedaddress']}</td>\n"; 
                        echo " <td>{$row['totalfinds']}</td>\n"; 
                        echo " <td><form action= locationsaction.php  method= 'post'><input type='hidden' name='lid' value=$theID />
                                    <input type= 'submit' name= 'type' value= 'View/Amend Location Details'/><input type= 'submit' name= 'type' value= 'Add/View Find Images'/><input type= 'submit' name= 'type' value= 'View Location Finds'/><input type= 'submit' name= 'type' value= 'View Location Finds'/></form></td>\n"; 
                        echo "</tr>\n"; 


                    }
                    ?>

【讨论】:

  • 这是一个好方法。您可以通过为输入提供唯一名称并检查存在而不是内容来使其更加灵活。我不相信这些年来文本会保持一致。 javascript-coder.com/html-form/html-form-submit.phtml
  • 嗨@BrianNickel,谢谢你。我以前使用过您的链接建议的方法,并且效果很好。正如你所说,这可能是我以后可能需要考虑的事情。再次感谢您和亲切的问候
【解决方案2】:

查看您的查询。由于您已对查询进行硬编码

$query = "SELECT  l.*, COUNT(f.locationid) totalfinds FROM detectinglocations l LEFT JOIN finds f ON f.locationid = l.locationid WHERE l.userid = '27' GROUP BY l.locationname";

在位置 ID 和用户 ID 中。这是一个假设,因为我看不到它们的行为是可变的。相反,它们是字符串。看看吧。

【讨论】:

  • 嗨@Shiv Kumar Ganesh,感谢您抽出宝贵时间回复我的帖子。我已经检查了独立添加变量而不是硬编码值的查询,不幸的是我仍然遇到同样的问题。亲切的问候
  • @IRHM 您的查询如何获取 $idnum ?从我在页面上看不到的地方? :(
  • @IRHM 我可以在隐藏的输入中看到位置 ID,但是查询中提到的 $idnum 的用户 ID 呢?
  • 嗨@Shiv Kumar Ganesh,感谢您在这方面的持续帮助。 '$idnum' 是一个会话 ID,它是在我的页面上的较早点检索到的。我不想发布一大堆代码,而是发布我遇到问题的部分。如果这没有帮助,我很抱歉。亲切的问候
  • @IRHM 抱歉,直到现在我都无法解决问题并提出多个问题。但是仍然告诉我,当您从会话中获取 $idnum 的值时,它确实只有一个值。当您单击发布时,仅评估该会话值而不是您单击的用户旁边的用户 ID 对吗?这就是为什么总是显示存储在 $idnum 中的用户值,并且您只看到以前的数据而不是新数据,因为单击提交时没有传递相关数据。希望我回答正确。如果不是请指出。
猜你喜欢
  • 2014-09-20
  • 2012-05-06
  • 1970-01-01
  • 1970-01-01
  • 2016-09-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多