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

时间:2014-08-23 08:20:11

标签: php mysql cookies

我想将特定的Uniq ID从MySQL行传递到另一个.php页面。但它仍然需要前20个记录并传递最后一个值,而不是仅存在于该行上的那个值。 Uniq ID是由autoireacment创建的,每个ID都是特定的,所以如果我点击第5行的链接,我想传递ID = 5。 有什么想法吗?

带有mysql表的文件:

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

第二档

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

2 个答案:

答案 0 :(得分:0)

更改

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['Uniq'] . "</td>";
setcookie("test", $row['Uniq'],time()+3600);
echo "<td><a href='invoice/index2.php'>Create Invoice</a></td>";

答案 1 :(得分:0)

实际上,你所做的是在每一个新行覆盖 cookie。如果你真的想要这样编码,我建议你给每个cookie一个特定的名字:

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>";
...
  }

但这似乎不是很聪明,因为cookiename已经显示了cookie的价值。因此,如果要将行的值传递给index2.php页面,最好使用查询字符串

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

请查看Wikipedia - Query StringPHP - $_GET了解详情。