0

如何使 topic_cat= 当前类别,例如http://prntscr.com/h08ae我希望将类别 ID 作为 topic_cat 插入。但是当我提出主题时,它只会导致类别 id = 0,这是我的代码:

 <?php

if($_SESSION['signed_in'] == false)
{header('Location: http://forums.pirate-zone.com/index.php?');
//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
            WHERE
                cat_id = " . mysql_real_escape_string($_GET['id']);

    $result = mysql_query($sql);

    if(!$result)
    {

        echo 'Error while selecting from database. Please try again later.';
    }
    else
    { $topic_cat=$row['cat_id'];
        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 '      
<div class="contain contentpadding">
<form action="" method="post">
<div style="margin: 10px 0 10px 120px">
<h3>Topic Name (required)</h3>
    <br/>
    <div class="light contentpadding inputdiv">
        <input type="text" name="topic_subject" size="50" />
    </div>
    <br/>
    <br/>



    <h3>Topic Body (required)</h3>
    </div>
<br/>
    <div class="light contentpadding inputdiv">
        <div class="cpadding">
<input type="button" class="bbbutton light" title="[color=][/color]" value="Color" />
<input type="button" class="bbbutton light" title="[size=11px][/size]" value="Size" />
<input type="button" class="bbbutton light" title="[b][/b]" value="Bold" />
<input type="button" class="bbbutton light" title="[i][/i]" value="Italic" />
<input type="button" class="bbbutton light" title="[u][/u]" value="Underline" />
<input type="button" class="bbbutton light" title="[img][/img]" value="Image" />
<input type="button" class="bbbutton light" title="[url=http://][/url]" value="URL" />
<input type="button" class="bbbutton light" title="[center][/center]" value="Center" />
<input type="button" class="bbbutton light" title="[quote=][/quote]" value="Quote" />
<input type="button" class="bbbutton light" title="[spoiler][/spoiler]" value="Spoiler" />
<input type="button" class="bbbutton light" title="  [youtube]http://www.youtube.com/watch?v=[/youtube]" value="YouTube Video" />
</div>
<div class="cpadding">
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/amazed.gif"  alt=":amazed" border="0" title=":amazed" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/amused.gif"  alt=":amused" border="0" title=":amused" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/blink.gif" alt=":blink" border="0" title=":blink" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/cheesy.gif" alt=":cheesy" border="0" title=":cheesy" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/confused.gif" alt=":confused" border="0" title=":confused" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/cool.gif" alt=":cool" border="0" title=":cool" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/cry.gif" alt=":cry" border="0" title=":cry" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/embarressed.gif" alt=":embarrassed" border="0" 

title=":embarrassed" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/evil.gif" alt=":evil" border="0" title=":evil" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/huh.gif" alt=":huh" border="0" title=":huh" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/laugh.gif" alt=":laugh" border="0" title=":laugh" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/mad.gif" alt=":mad" border="0" title=":mad" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/notrust.gif" alt=":notrust" border="0" title=":notrust" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/noworry.gif" alt=":noworry" border="0" title=":noworry" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/nuts.gif" alt=":nuts" border="0" title=":nuts" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/oh.gif" alt=":oh" border="0" title=":oh" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/push.gif" alt=":push" border="0" title=":push" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/rolleyes.gif" alt=":rolleyes" border="0" title=":rolleyes" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/sad.gif" alt=":sad" border="0" title=":sad" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/sick.gif" alt=":sick" border="0" title=":sick" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/smile.gif" alt=":smile" border="0" title=":smile" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/suspicious.gif" alt=":suspicious" border="0" title=":suspicious" 

/> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/teeth.gif"   alt=":teeth" border="0" title=":teeth" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/tounge.gif" alt=":tounge" border="0" title=":tounge" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/unsure.gif" alt=":unsure" border="0" title=":unsure" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/wacko.gif" alt=":wacko" border="0" title=":wacko" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/weird.gif" alt=":weird" border="0" title=":weird" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/wondering.gif" alt=":wondering" border="0" title=":wondering" /> 
<img class="bbemoticon" src="http://www.soul-boards.com/images/smilies/worried.gif" alt=":worried" border="0" title=":worried" />
<br />

</div>
<br/>           <center><textarea id="post_content" name="post_content"  cols="102" rows="13"></textarea></center>
    </div>
    <br/>


</div>
<div class="center">
        <input type="submit" value="Post Topic" />
    </div>
</form>
            </div>';
        }
    }
}
else
{
    //start the transaction
    $query  = "BEGIN WORK;";
    $result = mysql_query($query);

    if(!$result)
    {

        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['topic_subject']) . "',
                           NOW(),
                          '" . $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);

                //after a lot of work, the query succeeded!
                echo 'You have succesfully created <a href="topic.php?id='. $topicid . '">your new topic</a>.';
            }
        }
    }
}
}


?>   
4

1 回答 1

0

永远不要使用这种方法。不要在之前或之后回显header("location")

  header('Location: http://forums.pirate-zone.com/index.php?');
    //the user is not signed in
    echo 'Sorry, you have to be <a href="/forum/signin.php">signed in</a> to create a  topic.';
    }

并且您还没有获取mysql_query()Use this返回的数组

    while($row = mysql_fetch_assoc($result))
 {
 $topic_cat=$row['cat_id'];
 }
于 2012-10-08T04:53:28.997 回答