If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > question about select max(id) and mysql_insert_id

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-15-04, 18:10
gv631116 gv631116 is offline
Registered User
 
Join Date: Feb 2004
Posts: 1
question about select max(id) and mysql_insert_id

Hello all,
I have a PHP/Mysql program created by CodeCharge Studio has to submit two forms on one submit using Javascript, like:
function submitAll() {
document.forms["form1"].submit();
document.forms["form2"].submit();
}

form1 is to deal with the parent table which has an auto_increment id, called pid:
INSERT INTO parent_table VALUES('name1', 'address1');

After insertion to the parent table, I use mysql_insert_id() to get the latest id. I save it as a session variable $max_id.

from2 is to deal with the child table, which has its own primary key, called cid, all I wanna do is to save lastest pid to one of its columns.
INSERT INTO child_table (pid, item) VALUES($max_id, 'item1');

To ensure I can get the lastest id from parent_table, I even try to put " SELECT MAX(id) FROM parent_table " before INSERT to the child_table, and compared with the mysql_insert_id(). It works sometimes but sometimes not.

How can I get around of this problem? thanks.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On