0

我有一个索引页面,它链接到一个名为 categories.php 的页面。在 categories.php 中,论坛站点的每个不同类别都有一个基于其 id (cat_id) 的唯一 url。但是,我想将此 url 更改为基于其名称(cat_name)。但是,当我这样做时,我收到错误“无法显示类别,请稍后再试。'where 子句'中的未知列'thenameofacategory'”。这是因为 cat_id 是主键吗?我该如何更换

       <a href="category.php?id=' . $row['cat_id'] . '">

使用 cat_name 以便我不会在 category.php 页面上收到错误消息?

index.php 的摘录(重要部分):

        echo '<tr>';
            echo '<td class="leftpart">';
                echo '<h3><a href="category.php?id=' .                        $row['cat_id'] . '">' . $row['cat_name'] . '</a></h3>' . $row['cat_description'];
            echo '</td>';
            echo '<td class="rightpart">';

            //fetch last topic for each cat
                $topicsql = "SELECT
                                topic_id,
                                topic_subject,
                                topic_date,
                                topic_cat
                            FROM
                                topics
                            WHERE
                                topic_cat = " .  $row['cat_id'] . "
                            ORDER BY
                                topic_date
                            DESC
                            LIMIT
                                1";

                $topicsresult = mysql_query($topicsql);

                if(!$topicsresult)
                {
                    echo 'Last topic could not be displayed.';
                }
                else
                {
                    if(mysql_num_rows($topicsresult) == 0)
                    {
                        echo 'no topics';
                    }
                    else
                    {
                        while($topicrow = mysql_fetch_assoc($topicsresult))
                        echo '<a href="topic.php?id=' . $topicrow['topic_id'] . '">' . $topicrow['topic_subject'] . '</a> at ' . date('d-m-Y', strtotime($topicrow['topic_date']));
                    }
                }
            echo '</td>';
        echo '</tr>';
    }

这是页面 category.php:

      <?php
      //category.php
      include 'connect.php';


      //first select the category based on $_GET['cat_id']
      $sql = "SELECT
        cat_name,
        cat_id,
        cat_description
    FROM
        categories
    WHERE
        cat_id = " . mysql_real_escape_string($_GET['id']);

      $result = mysql_query($sql);

      if(!$result)
      {
echo 'The category could not be displayed, please try again later.' .                     mysql_error();
      }
      else
      {
  if(mysql_num_rows($result) == 0)
  {
    echo 'This category does not exist.';
  }
  else
  {
    //display category data
    while($row = mysql_fetch_assoc($result))
    {
        echo '<h2>Topics in &prime;' . $row['cat_name'] . '&prime;           category</h2><br />';
    }

    //do a query for the topics
    $sql = "SELECT  
                topic_id,
                topic_subject,
                topic_date,
                topic_cat
            FROM
                topics
            WHERE
                topic_cat = " .           mysql_real_escape_string($_GET['id']);

    $result = mysql_query($sql);

    if(!$result)
    {
        echo 'The topics could not be displayed, please try again later.';
    }
    else
    {
        if(mysql_num_rows($result) == 0)
        {
            echo 'There are no topics in this category yet.';
        }
        else
        {
            //prepare the table
            echo '<table border="1">
                  <tr>
                    <th>Topic</th>
                    <th>Created at</th>
                  </tr>';   

            while($row = mysql_fetch_assoc($result))
            {               
                echo '<tr>';
                    echo '<td class="leftpart">';
                        echo '<h3><a href="topic.php?id=' .           $row['topic_id'] . '">' . $row['topic_subject'] . '</a><br /><h3>';
                    echo '</td>';
                    echo '<td class="rightpart">';
                        echo date('d-m-Y',           strtotime($row['topic_date']));
                    echo '</td>';
                echo '</tr>';
            }
        }
    }
}
    }

    ?>

是不是因为 cat_id 是我不能在 WHERE 子句中使用 cat_name 作为 url 和 row 的主键?

谢谢!

4

1 回答 1

2

在 categories.php 中,更改

WHERE cat_id = " . mysql_real_escape_string($_GET['id']);

WHERE cat_name = '" . mysql_real_escape_string($_GET['id']) ."'";

问题是,在第一行代码中,cat_id 可能是一个整数,但 cat_name 是一个字符串。当你这样做时cat_id = $_GET['id'],它试图做cat_id = somecatname而不是cat_id = 'somecatname'

于 2012-06-03T19:16:00.107 回答