0

如标题所示,我收到“无法显示该主题,请稍后再试”。错误消息,但是数据可以正常插入到我的 mysql 表中,但我不知道为什么它们没有显示,这是我的结果页面。

<?php
include 'core/init.php';
include 'includes/overall/header.php';

$sql = "SELECT 'topic_id', 'topic_subject', 'category', 'sub_category', 'posted_by', 'posted', 'view', 'reply' FROM `topics`
    WHERE topics.topic_id = " . mysql_real_escape_string($_GET['id']);

$result = mysql_query($sql);

if(!$result)
{
echo 'The topic could not be displayed, please try again later.';
}
else
{
if(mysql_num_rows($result) == 0)
{
    echo 'This topic doesn&prime;t exist.';
}
else
{
    while($row = mysql_fetch_assoc($result))
    {
        $posts_sql = "SELECT 'posts.post_topic', 'posts.post_content', 'posts.post_date', 'posts.post_by', 
                             'users.user_id', 'users.username', 'users.profile'
                FROM
                    `posts`
                LEFT JOIN
                    `users`
                ON
                    posts.post_by = users.user_id
                WHERE
                    posts.post_topic = " . mysql_real_escape_string($_GET['id']);

        $posts_result = mysql_query($posts_sql);

        if(!$posts_result)
        {
            echo '<tr><td>The posts could not be displayed, please try again later.</tr></td></table>';
        }
        else
        {

            while($posts_row = mysql_fetch_assoc($posts_result))
            {
                include("includes/results-template.php");

            }
        }

        if(!$_SESSION['signed_in'])
        {
            echo '<tr><td colspan=2>You must be <a href="signin.php">signed in</a> to reply. You can also <a href="signup.php">sign up</a> for an account.';
        }
        else
        {
            //show reply box
            echo '<tr><td colspan="2"><h2>Reply:</h2><br />
                <form method="post" action="reply.php?id=' . $row['topic_id'] . '">
                    <textarea name="reply-content"></textarea><br /><br />
                    <input type="submit" value="Submit reply" />
                </form></td></tr>';
        }

        //finish the table
        echo '</table>';
    }
}
}
?>
<?php include 'includes/overall/footer.php'; ?>
4

1 回答 1

0

您不能选择带有单引号的列,而应该使用反引号选择它们。

将您的查询更改为

    $sql = "SELECT `topic_id`, `topic_subject`, `category`, `sub_category`, `posted_by`, `posted`, `view`, `reply`  FROM `topics`
WHERE topics.topic_id =  '".mysql_real_escape_string($_GET['id'])."' ";

并将此查询更改$posts_sql为:

        $posts_sql = "SELECT posts.post_topic, posts.post_content, posts.post_date, posts.post_by, users.user_id, users.username, users.profile
            FROM
                `posts`
            LEFT JOIN
                `users`
            ON
                posts.post_by = users.user_id
            WHERE
                posts.post_topic = '" . mysql_real_escape_string($_GET['id'])."' ";

反引号 ` 和单引号 ' 之间有区别。

于 2013-07-13T12:17:02.940 回答