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