Question

Basically I have edited the "new_topic" page in my generic forum so that the user can select the category of the post from a drop down list, rather than goto the category first and going to new topic, what I am trying to do now is when the thread is created, rather than jumping to the standard "your new thread was created here" it redirect to the category the topic was made under.

I tried using

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

But header was already being used elsewhere. So I found a meta refresh that works to an extent..

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

But it redirects to "category.phpid=" and outputs the following message.. The category could not be displayed, please try again later.You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 8

Is there anyway I can make the page jump to the category that is only being defined in the submit function itself? Thanks for any help.

EDIT: would using "if" functions work to define the redirect? ie. if topic_cat 2 is selected in the form redirect to category.phpid=2?? Sorry I am very new to PHP and still trying to get my head around it all :( Here is the topic creation page in it's entirety

<?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 ."'>";

            }
        }
    }
}
}
; ?>
Was it helpful?

Solution

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

That should work. You needed to put double quotation-marks outside the . $topic_cat . variable like so; " . $topic_cat . "

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top