gpt4 book ai didi

php - 回显 sql 行作为页面 url

转载 作者:行者123 更新时间:2023-11-29 08:52:13 24 4
gpt4 key购买 nike

我有一个索引页面,链接到名为categories.php 的页面。在categories.php 中,论坛站点的每个不同类别都有一个基于其id (cat_id) 的唯一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和行?

谢谢!

最佳答案

在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'

关于php - 回显 sql 行作为页面 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10872640/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com