2013-05-03 45 views
0

Thread_Body中的ThreadID需要是Thread_Titles id的ID。 我會如何去做這件事?在設置後立即獲取ID,在同一頁上

if (isset($_POST['Post_Thread'])) { 
    if (empty($_POST['Post_Title'])) { 
     echo "No Title No Thread."; 
    } else { 
     $Sec = mysql_real_escape_string($_GET['sec']); 
     mysql_query("INSERT INTO Thread_Titles (Name, Section, Posted, Poster, Updated) VALUES('".cleanPost($_POST['Post_Title'])."', '".$Sec."', '".time()."', '".$ULN->Username."', '".time()."') ") or die(mysql_error()); 
    } 
    if (empty($_POST['Post_Body'])) { 
     echo "No Body No Thread."; 
    } else { 
     mysql_query("INSERT INTO Thread_Replies (Body, Posted, Poster, ThreadID) VALUES('".cleanPost($_POST['Post_Body'])."', '".time()."', '".$ULN->Username."', '".$ThreadID."') ") or die(mysql_error()); 
    } 
} 

這是代碼都期望的形式。

回答

1

使用mysql_insert_id()

mysql_query("INSERT INTO Thread_Titles (Name, Section, Posted, Poster, Updated) VALUES('".cleanPost($_POST['Post_Title'])."', '".$Sec."', '".time()."', '".$ULN->Username."', '".time()."') ") or die(mysql_error()); 
$ThreadID = mysql_insert_id(); 

僅供參考,you shouldn't use mysql_* functions in new code。他們不再維護and are officially deprecated。請參閱red box?請改爲了解prepared statements,並使用PDOMySQLi - this article將幫助您決定哪個。如果您選擇PDO,here is a good tutorial

+0

我被告知這個,但我仍然不知道你們在說什麼。 我仍然學習很多,我走的視頻和東西,我從其他人那裏學習,所以... 也'mysql_insert_id()'會做什麼? – user2325910 2013-05-03 01:34:53

+1

點擊所有的鏈接,你會得到你所有的答案。 :) – 2013-05-03 01:36:53

+0

神聖的廢話,這很簡單?非常感謝你。 也感謝分享所有其他的東西,我現在將看看它。 – user2325910 2013-05-03 01:40:59

相關問題