2013-07-02 66 views
-1

基本上我在我的通用論壇中編輯了「new_topic」頁面,以便用戶可以從下拉列表中選擇帖子的類別,而不是先轉到類別,去新的話題,我現在要做的是線程創建時,而不是跳轉到標準的「你的新線程在這裏創建」它重定向到該主題下的類別。PHP/Mysql創建線程,然後重定向到論壇類別

我嘗試使用

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

但頭已經被用於其他地方。 所以我發現,工作在一定程度上元刷新..

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

但它重定向到「category.phpid =」而輸出下面的消息.. 類別無法顯示,請再試一次稍後。您的SQL語法中有錯誤;檢查與您的MySQL服務器版本相對應的手冊,以在第8行''附近使用正確的語法

是否有無論如何我可以讓頁面跳轉到只在submit函數本身定義的類別?謝謝你的幫助。

編輯:將使用「如果」功能工作來定義重定向?即。如果在重定向到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 ."'>"; 

      } 
     } 
    } 
} 
} 
; ?> 
+0

因爲回聲在雙引號中,變量應該插值而不用連接 –

回答

0

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

這應該工作,你需要把雙引號 - . $topic_cat .變量之外的標記像這樣; " . $topic_cat . "

+0

我剛剛嘗試過,我認爲問題在於它不知道topic_cat是什麼小號因此它正在加載一個空白空間並轉到URL category.php?id =並顯示「該類別無法顯示,請稍後再試。您的SQL語法中有錯誤;請檢查與您的MySQL服務器版本對應的手冊,以在第8行「'附近使用正確的語法」 – user2542256

+0

您能否告訴我第8行的文件中出現錯誤? –

+0

包括'connect.php'; 0_o – user2542256