Jump to content

Can you select and insert at the same time.


EchoFool

Recommended Posts

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! :(

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.