doushenyu2537 2013-08-07 21:59
浏览 56
已采纳

从SQL数据添加echo'dHT的链接

Back with another quick question. I have this code below which echo's out product names from a database. What I want to do is make the echoed out product names a link to another page called product.php, each link needs to have a unique ID, for example

<a href="product.php?id=1">Product Name</a> 

How would I go about doing this? Many thanks. I will point out that I am very new to 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;
?>
  • 写回答

1条回答 默认 最新

  • drnf593779 2013-08-07 22:02
    关注

    Assuming your Products table has a unique ID field called "id", change your select to:

    $rs = $conn->execute("SELECT id, product_name FROM Products");
    

    And when you want to create a link, use that field and pass it into the URL. So you'd have product.php?id=<?= $thatIdField; ?>.

    Example code:

    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>";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备