EchoFool Posted February 18, 2008 Share Posted February 18, 2008 Ok, i have a major problem here. Completely stuck as to how this works. I am designing a forum, now say the user creates a thread. This is then inserted into the thread table ...see below: <?php $UPDATE = mysql_query("INSERT INTO forums (Catergory,ThreadName,ThreadStarter,CreatedOn,LastPost, LastUser) VALUES ('$Category','$Subject','{$_SESSION['Current_User']}','$Date','$Date','{$_SESSION['Current_User']}')") Or die(mysql_error()); ?> Now the issue I have here is that i need to insert the "$Post" that the user made with the thread (obviously). Now I am sure you can see where this is going... as I insert a thread it creates a ID, but to insert a post i need this thread ID ... only I don't know how i can get the ID of the thread the user just created because theres no real way of searching for the ID .. you get me ? This is the real script... so you can see my problem: <?php $Post = mysql_real_escape_string($_POST['letter']); $Subject = mysql_real_escape_String($_POST['Subject']); //CREATE THREAD $UPDATE = mysql_query("INSERT INTO forums (Catergory,ThreadName,ThreadStarter,CreatedOn,LastPost, LastUser) VALUES ('$Category','$Subject','{$_SESSION['Current_User']}','$Date','$Date','{$_SESSION['Current_User']}')") Or die(mysql_error()); //POST FOR THE THREAD $UPDATE2 = mysql_query("INSERT INTO posts (Postmessage,postedby,CreatedOn,ThreadID) VALUES ('$Post','{$_SESSION['Current_User']}','$Date','---//not possible surely?---')") <?php Or die(mysql_error()); ?> Relating to this part in the second query "---//not possible surely?---" , so how can i get the ThreadID that was just created in order to post this at the same time. Is it possible to select threadID that is created in the first query in the forums table at the same time as the insert so it gets the correct ID? Hope some one can help me! Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted February 18, 2008 Share Posted February 18, 2008 You need to use mysql_insert_id(). So try: <?php $UPDATE2 = mysql_query("INSERT INTO posts (Postmessage,postedby,CreatedOn,ThreadID) VALUES ('$Post','{$_SESSION['Current_User']}','$Date', mysql_insert_id())")or die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
EchoFool Posted February 18, 2008 Author Share Posted February 18, 2008 You need to use mysql_insert_id(). So try: <?php $UPDATE2 = mysql_query("INSERT INTO posts (Postmessage,postedby,CreatedOn,ThreadID) VALUES ('$Post','{$_SESSION['Current_User']}','$Date', mysql_insert_id())")or die(mysql_error()); How will it know that it must match the same ThreadID as the primary key that is created upon insert on the first query? My first query table has a "ThreadID" field which when an insert occurs.. it auto increments. This then is the ID, if i use mysql_insert_id... won't it just pick any random number? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted February 18, 2008 Share Posted February 18, 2008 If you read the fine manual for mysql_insert_id(), you will see that it does what you want. Ken Quote Link to comment Share on other sites More sharing options...
EchoFool Posted February 18, 2008 Author Share Posted February 18, 2008 Argh right. Looks like they have thought of just about everything! Thanks poco and Ken! SOLVED Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.