繁体   English   中英

需要一些帮助的论坛代码mysql php

[英]need some assistance on a forum code mysql php

我有这个代码的问题

<?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行的强文本附近使用'' $ sql =“ SELECT cat_id,cat_name,cat_description从类别出于某种原因而无法正常使用的强文本**真的很感谢我不知道该怎么做的帮助,到处都是谷歌搜索,尝试了不同的组合,但是无法从cat_id获取ID,请帮助

似乎您没有正确的报价。 您应该将mysql_real_escape_string($_GET['id'])的结果括在'如下面的示例

"SELECT cat_id, cat_name, cat_description 
      FROM categories WHERE cat_id = '" . mysql_real_escape_string($_GET['id']) . "'";

如果要尝试使用简化的查询,请记住这种引号:

$sql= mysql_query("SELECT cat_id, cat_name, cat_description 
              FROM categories WHERE cat_id = '" .$id . "';"); 

但是为了调试尝试

var_dump($id); 

var_dump($sql);

并检查$ id是否包含正确的值以及$ sql的格式正确(尝试复制结果查询并在控制台中执行)

然后,如果查询在控制台中为您提供了正确的结果,则意味着结果查询是正确的。

PS的网址应该是

xxxxx.com/category.php?id=1

记得给你的ID分配一个值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM