-1

基本的に、一般的なフォーラムの「new_topic」ページを編集して、ユーザーが最初にカテゴリに移動して新しいトピックに移動するのではなく、ドロップダウン リストから投稿のカテゴリを選択できるようにしました。私が今やろうとしているのはスレッドが作成されると、標準の「新しいスレッドがここに作成されました」にジャンプするのではなく、トピックが作成されたカテゴリにリダイレクトされます。

使ってみた

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

しかし、ヘッダーはすでに他の場所で使用されていました。それで、ある程度機能するメタリフレッシュを見つけました..

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

しかし、「category.phpid=」にリダイレクトされ、次のメッセージが出力 されます。カテゴリを表示できませんでした。後でもう一度試してください。SQL 構文にエラーがあります。8 行目の near '' を使用する正しい構文については、MySQL サーバーのバージョンに対応するマニュアルを確認してください。

とにかく、送信機能自体でのみ定義されているカテゴリにページをジャンプさせることはできますか? 助けてくれてありがとう。

編集:「if」関数を使用してリダイレクトを定義できますか? すなわち。フォームで topic_cat 2 が選択されている場合、category.phpid=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 ."'>";

            }
        }
    }
}
}
; ?>
4

1 に答える 1