2012-10-08 104 views
0

如何使它成爲topic_cat =當前類別,例如http://prntscr.com/h08ae我希望類別id作爲topic_cat插入。但我當我提出它只是導致類別ID爲= 0的話題,下面是我的代碼:我該怎麼做才能使topic_cat =當前類別

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


?> 
+0

您可以輕鬆地添加'topic_cat'作爲查詢參數的任何鏈接 - 但你有*使用任何網頁*放在第一位變量?如果用戶使用該參數發出請求,那麼頁面必須*做些什麼來產生任何效果。 「 –

回答

0

不要使用此approach.Do不是之前或之後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()使用返回的數組這

while($row = mysql_fetch_assoc($result)) 
{ 
$topic_cat=$row['cat_id']; 
} 
+0

我收到 」插入數據時發生錯誤,請稍後重試 您的SQL語法錯誤;請查看與您的MySQL服務器版本相對應的手冊,以找到在'1)'附近使用的正確語法在第8行「當我添加代碼時。 – Malik

+0

@ user1592665它顯示你在sql中有錯誤query.find sql語法中的錯誤 – StaticVariable

相關問題