【发布时间】:2016-04-27 18:36:14
【问题描述】:
这段代码有问题
<?php
include '01.php';
include 'header.php';
$sql = "SELECT cat_id, cat_name, 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 ′' . $row['cat_name'] . '′ category</h2>';
}
//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><h3>';
echo '</td>';
echo '<td class="rightpart">';
echo date('d-m-Y', strtotime($row['topic_date']));
echo '</td>';
echo '</tr>';
}
}
}
}
}
include 'footer.php';
?>
它给我的错误是这个类别无法显示,请稍后再试。您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在第 1 行的 '' 附近使用 strong text ** $sql = "SELECT cat_id, cat_name, cat_description FROM categories WHERE 由于某种原因一直无法正常工作强文本** 非常感谢我不知道该怎么做的帮助,我到处搜索并尝试了不同的组合,但无法从 cat_id 获取 id 请帮助
【问题讨论】:
-
您从中获取 $_GET['id';] 的示例网址是什么?
-
xxxxx.com/category.php?id
-
PHP 中的
mysql_*函数已弃用,不应使用。请阅读Why shouldn't I use mysql_* functions in PHP?,了解为什么以及用什么来替换它们。 -
警告:如果您只是学习 PHP,请不要学习过时的
mysql_query接口。这很糟糕,已在 PHP 7 中删除。PDO is not hard to learn 之类的替代品和PHP The Right Way 之类的指南有助于解释最佳实践。 -
感谢您的建议,但我的期末作业还有不到 2 周的时间(不要问为什么),所以我无法真正重做大部分事情