Jump to content

Increasing a number from a DB


SephirGaine

Recommended Posts

I feel kind of lame about having 2 topics up on the forum, but at least it isn't the same question.

Through the help of some other topics and whatnot I managed to get my work order request form 99% finished. Now I just need to do one last thing, which is assign a work order number each time the script is ran. Here is how I see it working..

I create a new table on my database, named, say, 'WorkNum'. From there, I can recall the number, increase its value by 1, display it, assign the NEW number to a variable, and then store the variable and the new value.

However, if this IS the best way to do so, I've got a couple questions. First, I have no clue how to go about increasing a database entry's value. Sounds like it would fairly simple, however.

Second, instead of using INSERT is there something that will simply REPLACE a current entry in a database?
Link to comment
https://forums.phpfreaks.com/topic/16265-increasing-a-number-from-a-db/
Share on other sites

I'd use a simple approach.  Since each new work order produces a new entry in a database table, why not just use the record_id as the work number .. assuming you're using an auto-increment id in the table.  That makes finding things much simpler - work order 200 has data in record 200.
you're fighting against the grain with your method.

the simplest way of achieving this?  add an integer, auto-increment primary key to your work order table.  for example:

[code]ALTER TABLE work_orders ADD work_order_num INT UNSIGNED AUTO_INCREMENT, ADD PRIMARY KEY(work_order_num)[/code]

try running that in phpMyAdmin, or you can do this manually.  essentially it adds a field called 'work_order_num' to the start of the table fieldlist which is an auto increment integer.  what this means is that everytime a record is added to the table, it will have a unique work_order_num that is 1 greater than the last work_order_num used.  simply leave it out of your INSERT query and let MySQL generate it itself.

[code]INSERT INTO work_orders (field1, field2, field3) VALUES ('blah', 'blah', 'blah')[/code]

to fetch the work_order_num MySQL just assigned (on the last query run), use mysql_insert_id():

[code]$work_order_num = mysql_insert_id();[/code]

problem solved?

[b]EDIT:  AndyB has faster fingers than i.[/b]
[b]AWESOME.[/b] Unfortunately it hadn't even occured to me to create entries into a database for order requests, but I'll definitely have to do so, since this method seems that it would be a helluva lot easier than what I had in my head.

Thanks a TON for the help, guys! Much appreciated.

My edit: Quick update, got it working PERFECTLY. Once again, thanks for the help.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.