Welcome to Dream.In.Code
Become an Expert!

Join 149,459 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,118 people online right now. Registration is fast and FREE... Join Now!




Computed Field Inserting

 
Reply to this topicStart new topic

Computed Field Inserting, PHP/MySQL

10 Jul, 2002 - 11:53 AM
Post #1




Joined: --
Posts: 0

I'm trying to make an invoice number and insert it into the order table in the same query as when i create the order. The trick is I want to make the Invoice# based upon the order id.

Is it possible to insert something like ('company'.'year'.(id+1000)) where the id is the auto incremented id field in this same record?

H
User is offlinePM
+Quote Post

Cookie Mobster
RE: Computed Field Inserting
10 Jul, 2002 - 06:12 PM
Post #2

nooneenooneenooonee
Group Icon

Joined: 12 Oct, 2001
Posts: 4,723



Thanked: 1 times
Dream Kudos: 18
My Contributions
Wow great quandry! We know that you can insert the values from another field:
CODE

INSERT INTO mytable VALUES('0',23,name);

That will set both the filed "number" and the last field to 23, however if you did the same with an auto increment it will wite the value with the arbatrary temporary value you asigned in you script (in this case 0).

Now a little unknown function will help us out. LAST_INSERT_ID() will get the last AUTO_INCREMENTed value... We would just add one to in and we would have our auto_increment ID:
CODE

INSERT INTO mytable VALUES('0',34,LAST_INSERT_ID()+1);


Cheers Sam
User is offlineProfile CardPM
+Quote Post

RE: Computed Field Inserting
10 Jul, 2002 - 07:21 PM
Post #3




Joined: --
Posts: 0

I use mysql_insert_id() instead of last_insert_id, but I assume they do the same thing. I still don't know if I can add 1000000 to the id and concatenate the total on the end of a string inside the query. Tried a couple different ideas but it doesn't seem to work.

H
User is offlinePM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 01:48PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month