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 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了