dongshenyu4638 2014-08-23 08:20
浏览 32
已采纳

单击链接后从特定的mysql行传递变量

I'd like to pass specific Uniq ID from MySQL row to the another .php page. But it still takes 1st 20 records and pass the last value instead of just that one existing on the line. Uniq ID is created by autoireacment and its specific for every so I'd like to pass ID=5 if I click the link on the line 5. Any ideas please?

the file with mysql table:

session_start();
require "login.php";
$con= mysqli_connect($server, $user, $pass, $db);
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM $table");
echo "<table border='1'>
...
while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['Uniq'] . "</td>";
  echo "<td>" . '<a href="invoice/index2.php">Create Invoice</a>';setcookie("test", $row['Uniq'],time()+3600) . "</td>";
  echo "<td>" . $row['Date'] . "</td>";
...
  }
echo "</table>";
mysqli_close($con);

2nd file

session_start(); 
require "../login.php"; 
echo $_COOKIE["test"];
  • 写回答

2条回答 默认 最新

  • dptn69182 2014-08-23 08:34
    关注

    Actually, what you were doing is overwriting the cookie at every new row. If you really want to code like that, I would recommend you to give every cookie a specific name:

    while($row = mysqli_fetch_assoc($result))
      {
      echo "<tr>";
      echo "<td>" . $row['Uniq'] . "</td>";
      echo "<td>" . '<a href="invoice/index2.php">Create Invoice</a>';setcookie("test".$row['Uniq'], $row['Uniq'],time()+3600) . "</td>";
      echo "<td>" . $row['Date'] . "</td>";
    ...
      }
    

    But that seems not very smart since the cookiename already shows the value of the cookie. So it would be better maybe to work with query strings if you want to pass the value of the row to the index2.php page.

    while($row = mysqli_fetch_assoc($result))
      {
      echo "<tr>";
      echo "<td>" . $row['Uniq'] . "</td>";
      echo "<td>" . '<a href="invoice/index2.php?uniq='.$row['Uniq'].'">Create Invoice</a>'."</td>";
      echo "<td>" . $row['Date'] . "</td>";
    ...
      }
    
    //in index2.php
    if(isset($_GET['uniq'])) {
       $uniq = $_GET['uniq'];
       echo 'The ID is: '.$uniq;
    }
    

    Take a look at Wikipedia - Query String and PHP - $_GET to learn more.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。