PHP / Mysql创建线程然后重定向到论坛类别

时间:2013-07-02 11:04:33

标签: php mysql forum

基本上我已经编辑了" new_topic"我的通用论坛中的页面,以便用户可以从下拉列表中选择帖子的类别,而不是首先转到类别并转到新主题,我现在要做的是创建线程时,而不是跳到标准"你的新线程在这里创建了#34;它重定向到主题所在的类别。

我尝试使用

header( "Location: category.php?id='. $topic_cat . '" );

但是标题已经被其他地方使用了。 所以我发现元刷新在某种程度上起作用..

echo "<META HTTP-EQUIV='Refresh' Content='0; URL=/category.php?id='. $topic_cat .'>";

但它重定向到&#34; category.phpid =&#34;并输出以下消息.. 无法显示该类别,请稍后再试。您的SQL语法中有错误;查看与您的MySQL服务器版本相对应的手册,以获得在&#39;&#39;附近使用的正确语法。第8行

无论如何,我可以让页面跳转到仅在提交功能本身中定义的类别吗?谢谢你的帮助。

编辑:会使用&#34;如果&#34;函数用于定义重定向?即。如果在重定向到category.phpid = 2的形式中选择topic_cat 2 对不起,我是非常新的PHP,仍然试图让我的头围绕这一切:( 这是其中的主题创建页面

<?php
//create_topic.php
include 'connect.php';

if($_SESSION['signed_in'] == false)
{
//the user is not signed in
echo 'Sorry, you have to be <a href="/forum/signin.php">signed in</a> to create a     topic.';
}
else
{
//the user is signed in
if($_SERVER['REQUEST_METHOD'] != 'POST')
{   
    //the form hasn't been posted yet, display it
    //retrieve the categories from the database for use in the dropdown
    $sql = "SELECT
                cat_id,
                cat_name,
                cat_description
            FROM
                categories";

    $result = mysql_query($sql);

    if(!$result)
    {
        //the query failed, uh-oh :-(
        echo 'Error while selecting from database. Please try again later.';
    }
    else
    {
        if(mysql_num_rows($result) == 0)
        {
            //there are no categories, so a topic can't be posted
            if($_SESSION['user_level'] == 1)
            {
                echo 'You have not created categories yet.';
            }
            else
            {
echo 'Before you can post a topic, you must wait for an admin to create some categories.';
            }
        }
        else
        {

            echo '<form method="post" action="">'; 

            echo '<select name="topic_cat">';
                while($row = mysql_fetch_assoc($result))
                {
                    echo '<option value="' . $row['cat_id'] . '">' . $row['cat_name'] . '</option>';
                }
            echo '</select><br />'; 

            echo '<textarea name="post_content" /></textarea><br /><br />
                <input type="submit" value="Create topic" />
             </form>';
        }
    }
}
else
{
    //start the transaction
    $query  = "BEGIN WORK;";
    $result = mysql_query($query);

    if(!$result)
    {
        //Damn! the query failed, quit
        echo 'An error occured while creating your topic. Please try again later.';
    }
    else
    {

        //the form has been posted, so save it
        //insert the topic into the topics table first, then we'll save the post into the posts table
        $sql = "INSERT INTO 
                    topics(topic_subject,
                           topic_date,
                           topic_cat,
                           topic_by)
               VALUES('" . mysql_real_escape_string($_POST['post_content']) . "',
                           NOW(),
                           " . mysql_real_escape_string($_POST['topic_cat']) . ",
                           " . $_SESSION['user_id'] . "
                           )";

        $result = mysql_query($sql);
        if(!$result)
        {
            //something went wrong, display the error
            echo 'An error occured while inserting your data. Please try again later.<br /><br />' . mysql_error();
            $sql = "ROLLBACK;";
            $result = mysql_query($sql);
        }
        else
        {
            //the first query worked, now start the second, posts query
            //retrieve the id of the freshly created topic for usage in the posts query
            $topicid = mysql_insert_id();

            $sql = "INSERT INTO
                        posts(post_content,
                              post_date,
                              post_topic,
                              post_by)
                    VALUES
                        ('" . mysql_real_escape_string($_POST['post_content']) . "',
                              NOW(),
                              " . $topicid . ",
                              " . $_SESSION['user_id'] . "
                        )";
            $result = mysql_query($sql);

            if(!$result)
            {
                //something went wrong, display the error
                echo 'An error occured while inserting your post. Please try again later.<br /><br />' . mysql_error();
                $sql = "ROLLBACK;";
                $result = mysql_query($sql);
            }
            else
            {
                $sql = "COMMIT;";
                $result = mysql_query($sql);

echo "<META HTTP-EQUIV='Refresh' Content='0; URL=/category.php?id=". $topic_cat ."'>";

            }
        }
    }
}
}
; ?>

1 个答案:

答案 0 :(得分:0)

echo "<META HTTP-EQUIV='Refresh' Content='0; URL=/category.php?id=" . $topic_cat . "'>";

那应该有用。你需要在. $topic_cat .变量之外加上双引号; " . $topic_cat . "