Php 将sql行作为页面url进行回显

Php 将sql行作为页面url进行回显,php,mysql,sql,where-clause,Php,Mysql,Sql,Where Clause,我有一个索引页面,链接到categories.php中名为categories.php的页面,论坛站点的每个不同类别都有一个基于其id(cat_id)的唯一url。但是,我想将此url更改为基于其名称(cat_名称)。但是,当我执行此操作时,我收到错误“类别无法显示,请稍后重试。未知列‘where子句’中的‘thenameofacategory’”。这是因为cat_id是主键吗?我如何更换新的 <a href="category.php?id=' . $row['cat_id

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

       <a href="category.php?id=' . $row['cat_id'] . '">
”$行['cat_description'];
回声';
回声';
//获取每只猫的最后一个主题
$topicsql=“选择
主题id,
主题(主题),
主题(日期),
主题猫
从…起
话题
哪里
主题_cat=“”$行['cat_id']。"
订购人
主题日期
描述
极限
1";
$topicsresult=mysql\u查询($topicsql);
如果(!$topicsresult)
{
echo“无法显示最后一个主题”;
}
其他的
{
if(mysql_num_rows($topicsresult)==0)
{
回应“没有话题”;
}
其他的
{
while($topicrow=mysql\u fetch\u assoc($topicsresult))
回音'at'。日期('d-m-Y',标准时间($topicrow['topic_date']);
}
}
回声';
回声';
}
下面是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是主键,所以我不能使用cat_name作为WHERE子句中的url和行吗


谢谢

在categories.php中,更改

WHERE cat_id=“.mysql_real_escape_字符串($\u GET['id']);

其中cat_name='”。mysql_real_escape_字符串($_GET['id'])。“”


问题是,在第一行代码中,cat_id可能是一个整数,但cat_name是一个字符串。当您执行
cat\u id=$\u GET['id']
时,它试图执行
cat\u id=somecatname
而不是
cat\u id='somecatname'

我建议您也签出此库-它将执行与您的代码现在执行的所有操作相同的操作,但是它有很好的特性,可以非常安全地准备查询,并且非常严格地执行操作。只是一个想法!
      <?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>';
            }
        }
    }
}
    }

    ?>