Jump to content

Inserting same data into 2 tables at once, but field 1 is auto-incremement


Recommended Posts

I have 2 tables with the same fields. Table 1 is the master list, table 2 is being used as an audit log.

 

Here's the problem: when I create a new record and insert it into table 1, there is a numeric field that is set to auto-increment and gets automatically populated in table 1, so it's a unique number that no one knows until the record is submitted into the db.

 

But since I have the PHP written to submit this data into 2 tables at the same time, that field in table 2 doesn't get updated.

 

How do I grab that auto-incremented field from table 1 and make sure that field gets copied into table 2?

mysql_insert_id()

 

But if table 2 is an audit of anything that happens in table 1, is there any reason you're not doing this with triggers?  Otherwise, you should really be doing this transactionally to deal with failures so you're not populating one table and not the other.

Setting it up with triggers is much more complicated (for me) and time consuming. This is the quick and dirty way and does exactly what I want (copies all info into a single row). Except for this one part when I insert a new item.

If you want to do it dirty like you say, after you insert the data into table one, run a query right after that and see what the last one was you inserted. Like this -

 

$sql = "SELECT num FROM table ORDER BY num desc limit 1";

$result = mysql_query($sql);

 

Then turn around and insert that back into to table 2.

theres no idea to write another query to get the value in the auto increment column of the last insert

 

you can get it like this:

 

$id = mysql_insert_id();

 

OR, this:

 

$res = mysql_query(" LAST_INSERT_ID() ")or die(mysql_error());

Bobcat's code didn't work. :( Seems like it would, but it returns a 0. I'll try ohdang's.

 

EDIT: That doesn't work either - getting an error on the LAST_INSERT line. My field is called ITEM_NUM, so I have LAST_INSERT_ITEM_NUM. Not sure if that's what's causing the problem.

Regarding the LAST_INSERT_ID() - I'm doing this:

 

$itemnum = mysql_query("LAST_INSERT_ID() ")or die(mysql_error());

 

And then inserting the new data into table2 and calling for $itemnum. But I'm still getting an error on the LAST_INSERT code.

 

 

 

Bobcat:

 

I tried this:

 

$itemnum = "mysql_query(SELECT Item_Num FROM TABLE1 ORDER BY Item_Num DESC LIMIT 1)";

 

But it still returns a 0.

Got it. Here's what I had to do:

 

<?php 
$itemlast = mysql_query("SELECT Item_Num FROM TABLE1 ORDER BY Item_Num DESC LIMIT 1") or die(mysql_error());
while($item=mysql_fetch_array($itemlast))
{	
$itemnum=$item["Item_Num"];
}?>

 

Then I plugged in $itemnum where I wanted the number to appear. I used Bobcat's code and grabbed some code I used elsewhere on the site to make it work.

 

Thanks for the help everyone! You all gave me a good starting place to get this fixed.

You built in a race condition there.  If you have two inserts that happen at the same time, that $itemlast value is going to be the same higher value for both threads processing this statement, referencing whichever one MySQL processed first.

 

mysql_insert_id() in PHP or LAST_INSERT_ID() in MySQL both stay within the current connection handle, which is unique to each thread and won't change unless you tell it to while processing your script, so it eliminates this race condition.

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.