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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器