0

回到另一个快速的问题。我在下面有这段代码,它从数据库中回显了产品名称。我想要做的是将回显的产品名称作为指向另一个名为 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;
?>
4

1 回答 1

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>";
于 2013-08-07T22:02:18.060 回答