来自数据库的PHP超链接


php hyperlinks from database

我对php和数据库程序很陌生,但正在努力学习这门语言并为我的库开发一个系统。我有一个名为booksdb的数据库和两个名为"books"的表,其中包含图书和作者的标题,bookcatname包含具有唯一类别ID的图书类别。

我想做的是,在页面上显示所有类别,当一个类别链接被点击时,我想看到该特定类别下的书籍和作者的名字显示在另一个页面上,

的例子:

Art;
    Color and Light by James Gurney
    The Art Spirit by Robert Henry
    How Pictures Work by David Bayles
    Imaginative Realism by James Gurney

这是我的代码,但它不工作。

<?php 
  $dbh=mysql_connect("localhost","root","root") or die ('Cannot connedt to the Database' .mysql_errno()); 
  mysql_select_db("booksdb");

  //$res = "SELECT * FROM bookstable GROUP BY category ORDER BY category ASC";
  $res = "SELECT * FROM bookcatname ORDER BY category ASC";

  $res_query = mysql_query($res) or die (mysql_error());
  $ra = mysql_fetch_assoc($res_query);  
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Select a Company</title>
  </head>
  <body>
  <?php do { ?>
    <p><a href="page.php?cat_id=<?php echo $ra['cat_id']; ?>"><?php echo $ra['category']; ?></a></p>
  <?php } while ($ra = mysql_fetch_assoc($res_query))?>
  </body>
</html>

1.Table name - bookcatname
+----+--------+----------+
| id | cat_id | category |
+----+--------+----------+
|  1 |      1 | Art      |
|  2 |      2 | Drama    |
|  3 |      3 | Music    |
|  4 |      4 | Fiction  |
|  5 |      5 | Computer |
+----+--------+----------+
2.Table name - books
+----+--------+---------------------------------+-----------------------+
| id | cat_id | title                           | author                |
+----+--------+---------------------------------+-----------------------+
|  1 |      1 | Color and Light                 | James Gurney          |
|  2 |      1 | The Art Spirit                  | Robert Henry          |
|  3 |      1 | Art & Fear                      | David Bayles          |
|  4 |      1 | How Pictures Work               | Molly Bang            |
|  5 |      1 | Imaginative Realism             | James Gurney          |
|  6 |      2 | A Walk To Remember              | Nicholas Sparks       |
|  7 |      2 | An Old Fashioned Girl           | Louisa May Alcott     |
|  8 |      3 | The Rest Is Noise               | Alex Ross             |
|  9 |      3 | It Still Moves                  | Amanda Petrusich      |
| 10 |      3 | Chronicles                      | Bob Dylan             |
| 11 |      3 | Dream Boogie                    | Peter Guralnick       |
| 12 |      3 | Escaping The Delta              | Robert Johnson        |
| 13 |      4 | Atlas Shrugged                  | Ayn Rand              |
| 14 |      4 | Anthem                          | Ayn Rand              |
| 15 |      4 | Sons and Lovers                 | D.H. Lawrence         |
| 16 |      4 | Henderson the Rain King         | Saul Bellow           |
| 17 |      5 | The Art of Computer Programming | Donald Knuth          |
| 18 |      5 | The Art of Unix Programming     | Eric Raymond          |
| 19 |      5 | Free Software, Free Society     | Richard M. Stallman   |
| 20 |      5 | Database System Concepts        | Abraham Silberschatz  |
| 21 |      5 | 3ds Max 2008 in Simple Steps    | Kognet Solutions Inc. |
+----+--------+---------------------------------+-----------------------+

您当前显示类别的代码不工作,因为您使用:

$ra = mysql_fetch_assoc($res_query);

,然后在循环中再执行一次:

while ($ra = mysql_fetch_assoc($res_query)

只做一个while()来获取类别:

$getCategories = mysql_query("SELECT * FROM bookcatname ORDER BY category ASC");
while ($category = mysql_fetch_assoc($res_query) )
{
  echo '<a href="page.php?cat_id='.$category['cat_id'].'">'.$category['category'].'</a>';
}

现在,您将需要另一个页面从该类别中获取书籍(或者您可以在同一页面中进行)。

books.php

// Same stuff here to connect database
// you check if a category is sent, else you redirect to categories page.
if ( empty($_GET['cat_id']) )
{
  header('Location: index.php');
  exit();
}
// Now you get books from category
// intval() convert to number
$getBooks = mysql_query("SELECT * FROM books WHERE cat_id = '".intval($_GET['cat_id'])."'");
echo '<ul>';
while ( $book = mysql_fetch_assoc($getBooks) )
{
   echo '<li>'.$book['title'].' by '.$book['author'].'</li>';
}
echo '</ul>';