473,667 Members | 2,577 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with last_insert_id( )

7 New Member
I am trying to insert an auto_increment value into multiple tables but its seems to lose its value after the the second insert statement and my code is as follows can any1 please help. The auto_increment value that i want to use is the one from the client table and it will insert in the CONTACTS table but it will not in the JOBSHEET and KNOWLEDGE table. last_insert_id( ) has not worked because i have auto increment fields in all the tables pliz pliz help

mysql_query("IN SERT INTO CLIENT VALUES(
NULL, '$companyname', '$contactname', '$siteaddress', '$phonenumber', NULL, '$website', '$code', '$postaladdress ')") or die (mysql_error()) ;

mysql_query('SE T @clientID = last_insert_id( )') or die (mysql_error()) ;




mysql_query("IN SERT INTO CONTACTS VALUES( NULL, '@clientID' )") or die (mysql_error()) ;



mysql_query("IN SERT INTO JOBSHEET VALUES( NULL, '@clientID')") or die (mysql_error()) ;


mysql_query("IN SERT INTO KNOWLEDGE VALUES( NULL, '@clientID', '$categoryID', '$staffID', '$knowledge')") or die (mysql_error()) ;
Nov 27 '06 #1
1 4111
ronverdonk
4,258 Recognized Expert Specialist
MySQL doc: The currently executing statement does not affect the value of LAST_INSERT_ID( ). Suppose that you generate an AUTO_INCREMENT value with one statement, and then refer to LAST_INSERT_ID( ) in a multiple-row INSERT statement that inserts rows into a table with its own AUTO_INCREMENT column. The value of LAST_INSERT_ID( ) will remain stable in the second statement; its value for the second and later rows is not affected by the earlier row insertions. (However, if you mix references to LAST_INSERT_ID( ) and LAST_INSERT_ID( expr), the effect is undefined.)

Sample from M ySQL docs: If you want store the last id for use in multiple insert statements:
Expand|Select|Wrap|Line Numbers
  1. create table person (
  2. id bigint not null primary key auto_increment,
  3. name char(100) not null
  4. );
  5. insert into person set name = 'Joe';
  6. select @id:=id as id from class where id = last_insert_id();
  7. insert into some_other_table set person_id = @class_id;
  8. insert into yet_another_table set person_id = @class_id;
Ronald :cool:
Jan 17 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
7314
by: Irene Mettias | last post by:
Hello, I've just installed perl DBI v1.38 which according to the documentation supports the method last_insert_id(). For some reason the return value I get is undef. Here is my source code : #!/usr/local/bin/perl
1
4271
by: Michal Adamczakk | last post by:
hi, how to implement mysql's last_insert_id() ? i know that seqences and oids are great. the one thing i miss is that they are not session specific. i mean selecting last_value from seqence can give me a value which was inserted by a different user.
6
12667
by: jim | last post by:
Hi, I have a site with various parts which allow users to enter info via forms which gets entered into various tables in a MySQL db (I'm using ASP, rather than PHP). I have an ecards bit, and when users send a card, their data is entered into the db via something like: INSERT vars into ecard_tbl....
3
41660
by: Vic Spainhower | last post by:
Hello, I am trying to get the value of the Auto-Incremented key that was assigned using LAST_INSERT_ID(). However, when I execute the following PHP statements after the insert I only get: "value of entryID: Resource id #7" after EVERY insert (it never changes). What am I doing wrong? Thanks very much!
1
9600
seshu
by: seshu | last post by:
thank sir now one more dubt how to use last_insert_id( ) because i want get all the last inserted and then update only those values can you say me how? regards seshu
3
2967
by: jx2 | last post by:
hi guys i would appriciate your coments on this code - when i ran it for the very first time it doesnt see @last = LAST_INSERT_ID() but when i ran it next time it read it properly i need to know it imiedietely after i insert value into session1... is there any other way to do it? insert into 2 tables at the same time ...? if(!($sessid)){ session_register('sessid'); mysql_query("insert into session1 set sessid=null,
2
2962
by: WebSnozz | last post by:
I am losing the leading zeros when I get autoincrement IDs from a column with ZEROFILL enabled. I would like to have the leading zeros. I have tried both the php function last_insert_id as well as running a SELECT query and using the result. I am getting the correct id, it is just losing the leading zeros. I have doulbe checked the table in the database, and it does have the leading zeros. Thanks for any help. $result =...
2
4376
by: wizardry | last post by:
Hello - I'm recently a newbie to mysql, but have experiance in oracle, mssql, access, etc... Here is my problem, i've created the table needed to test the audit procedures. the data element that the id is going to be inserted into is auto_increment. I don't know what the problem could be. here is the table code:
5
3084
Markus
by: Markus | last post by:
The MySQL manual states: I wonder what is considered a 'client'. Is that simply just different connections to the database?
0
8457
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8365
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8883
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8788
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8563
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8646
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7390
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2776
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.