Assigning ID to form data BEFORE posting

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

Assigning ID to form data BEFORE posting

Post by michlcamp »

Is there a way to assign an ID to form data before it's posted to a data table?

I have a form which collects data for product sales but want to automatically assign the order_id value before the form data is posted - in other words, not using an 'auto-incrementing' field in the table as it posts. (I'm using that for something else).

The order_id value would have to be the NEXT number after the previous order.

and, is there a way I can pass that value to the next form using a hidden field?


thanks in advance,
mc
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

You'll have to query to db to find the latest row,

ie..

Code: Select all

SELECT MAX(`order_id`) FROM ....
Although from what I can tell, this doesn't really seem like a good table design to me..
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

Post by michlcamp »

yeah, I don't think so either...I'm thinking more along the lines of keeping an appendable text file that stores the order_id's - reading it when the form page comes up, assigning the next consecutive number to the order being filled out, then posting that order_id to the table.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

michlcamp wrote:yeah, I don't think so either...I'm thinking more along the lines of keeping an appendable text file that stores the order_id's - reading it when the form page comes up, assigning the next consecutive number to the order being filled out, then posting that order_id to the table.
You're gonna get into an almighty tangle with that.... if two people access the page at the same sort of time there's a fair chance you'll collide the ID's. Why can't you have another auto-increment column?
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

Post by michlcamp »

agreed again - wanted to create the idea and send the order_id from form to form before posting the order to the database. Trying to avoid having to post then query then post again (or update/insert, whichever the case may be).

The whole thing centers around a second set of forms where additional order info is posted to a different data table.

I'll just go the safe route here I guess
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

I would probably have a seperate table for storing the next available product order, and base my id's off that.
Post Reply