Jump to content


Increasing a number from a DB

  • Please log in to reply
4 replies to this topic

#1 SephirGaine

  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts

Posted 01 August 2006 - 10:58 PM

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?

#2 Caesar

  • Members
  • PipPipPip
  • Advanced Member
  • 1,025 posts

Posted 01 August 2006 - 11:03 PM

mysql_query("UPDATE table SET field=$value WHERE id='1'");

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.
PHP Ninja

#3 AndyB

  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 01 August 2006 - 11:03 PM

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.
Legend has it that reading the manual never killed anyone.
My site

#4 akitchin

  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 01 August 2006 - 11:06 PM

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:

ALTER TABLE work_orders ADD work_order_num INT UNSIGNED AUTO_INCREMENT, ADD PRIMARY KEY(work_order_num)

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.

INSERT INTO work_orders (field1, field2, field3) VALUES ('blah', 'blah', 'blah')

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

$work_order_num = mysql_insert_id();

problem solved?

EDIT:  AndyB has faster fingers than i.

#5 SephirGaine

  • Members
  • PipPipPip
  • Advanced Member
  • 44 posts

Posted 01 August 2006 - 11:13 PM

AWESOME. 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.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users