回到另一个快速问题。我在下面有这个代码,它回显了数据库中的产品名称。我想要做的是使回显的产品名称成为另一个名为product.php的页面的链接,每个链接都需要有一个唯一的ID,例如
<a href="product.php?id=1">Product Name</a>
我该怎么做呢?非常感谢。我会指出我是PHP的新手。
<?php
//create an ADO connection and open the database
$conn = new COM("ADODB.Connection");
$conn->open("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=C:\WebData\Northwind.mdb");
//execute an SQL statement and return a recordset
$rs = $conn->execute("SELECT product_name FROM Products");
$num_columns = $rs->Fields->Count();
echo "<table border='1'>";
echo "<tr><th>Name</th></tr>";
while (!$rs->EOF) //looping through the recordset (until End Of File)
{
echo "<tr>";
for ($i=0; $i < $num_columns; $i++) {
echo "<td>" . $rs->Fields($i)->value . "</td>";
}
echo "</tr>";
$rs->MoveNext();
}
echo "</table>";
//close the recordset and the database connection
$rs->close();
$rs = null;
$conn->close();
$conn = null;
?>
答案 0 :(得分:0)
假设您的Products表具有名为“id”的唯一ID字段,请将您的选择更改为:
$rs = $conn->execute("SELECT id, product_name FROM Products");
当您想要创建链接时,请使用该字段并将其传递到URL中。所以你有product.php?id=<?= $thatIdField; ?>
。
示例代码:
echo "<table border='1'>";
echo "<tr><th>Name</th></tr>";
while (!$rs->EOF) //looping through the recordset (until End Of File)
{
echo "<tr>";
for ($i=0; $i < $num_columns; $i++) {
echo "<td><a href=\"product.php?id=" . $rs->Fields('id').value . "\">" . $rs->Fields($i)->value . "</a></td>";
}
echo "</tr>";
$rs->MoveNext();
}
echo "</table>";