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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料