【问题标题】:$_SESSION problem - I have same ID on all <tr>$_SESSION 问题 - 我在所有 <tr> 上都有相同的 ID
【发布时间】:2021-06-18 16:22:54
【问题描述】:

我不知道,我是怎么做到的:我想将数据库中的$id 添加到$_SESSION["dbID"],点击后,它会显示来自数据库的更多信息。但是表在while函数中生成,$_SESSION["dbID"]每次设置为表中的最高行数。拜托,任何人都可以更改我的代码,因为我在每个&lt;tr&gt; 的表上都有$_SESSION["dbID"] 不同吗?谢谢

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

【问题讨论】:

  • 您不想为此使用 SESSION,您想将 id 添加到链接中
  • 但是为什么每一行都有多个链接呢?以及 tr? 中的 href 属性?
  • 哎,我只测试一下,能不能给tr加href,这样以后就不用给所有td加href了。但这行不通。还有 JS Query。你知道解决方案吗?
  • 我建议一点 JS 魔法
  • 好的。解决了。但我不知道,我是如何在每一行得到不同的 $id 的。请问你知道怎么做吗?

标签: php html mysql session tr


【解决方案1】:
if (mysqli_num_rows($sql) > 0) {
  $row = mysqli_fetch_assoc($sql);
}

while($row = $result->fetch_assoc()){ ?>

    <tr class="trX" id="'.<?php echo $row['ID']; ?>.'" href="google.com&id='.$row['ID'].'">
        <td class="tdX"><a style="color:black; text-decoration: none;" href="page.php"><?php echo $row['ID']; ?></a></td>
        <td class="tdX"><a style="color:black; text-decoration: none;" href="page.php"><?php echo $row['Name']; ?></td>
        <td class="tdX"><a style="color:black; text-decoration: none;" href="page.php"><?php echo $row['Subject']; ?></td>
        <td class="tdX"><a style="color:black; text-decoration: none;" href="page.php"><?php echo $row['Date']; ?> </td>
    </tr>

<?php } ?>

不要定义行,只需在数据库中调用它们即可。

【讨论】:

  • 此解决方案无效。如果 tr 不在 php 中,它会向我显示任何内容。我不知道为什么。
  • @MartanKing 尝试更新的解决方案,看看它是否有效
  • 我使用了@Steven 的其他解决方案,但谢谢 :)
【解决方案2】:

说明

您可以取消所有a 标签并使用JavaScript 来处理重定向...

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

将 URL 设置为要链接到的页面。上面的行显示了服务器上文件“file.php”的链接,查询字符串为“id=$id”。

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

上面的行是 href 的 JS 等价物。如果您想导航到域外的服务器,请记住添加完整的 url,例如https://www.website.com

代码示例

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

【讨论】:

  • 是的。但是 $id 仍然是最大 ID 数。我是怎么做到的,我想在每一行上使用不同的 $id,不一样...?
  • 这将为您在每一行提供不同的 ID。您不再使用 SESSION 而是使用 GET
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-08-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多