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
Share on other sites


[code]<?php
mysql_query("UPDATE table SET field=$value WHERE id='1'");
?>[/code]

Also...how you assign your work order nmumbers depends on the approach you take. If you use the row's/record's "id" field as the wo#...then you can simply set the field to auto increment.
Link to comment
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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

[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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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