Relating New Rows In 2 Different Tables?
#1
Posted 28 December 2012 - 07:47 PM
I'm not sure if this is possible, i'm a semi noob so go easy on me!
I have 2 tables, Clients & Jobs. When I post a new Job, i've got a page that adds jobs as a new row, with the chosen Client ID, so for example if the Client ID is 'CLIENT007' and the job number is '007', In my Jobs table, I get "ClientID: CLIENT007; JobID: 007;"
Hope this makes sense so far. Anyway is there a way of me finding the last job number and incrementing the next job? So instead of having to enter a JobID it will do something like:
Client ID = CLIENT007 last JobID = 007, so next JobID is 008.
Bare in mind that I can't simply auto increment the job ID as I could have CLIENT002 with JobID 123.
Hope this makes some sort of sense!!
Many thanks in advanced!
#2
Posted 28 December 2012 - 07:48 PM
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
#3
Posted 28 December 2012 - 07:53 PM
I have an auto-increment primary key, but the ID I want to relate is related to my filing system. For example I have client ID's, and then job id's that follow. For example CLIENT007's first job id is job 1, then the next job is job 2. If I use the auto-increment on the jobs table, then I have a situation where it could go:
JobID: 1; Client: CLIENT007;
JobID: 2; Client: CLIENT002;
JobID: 3; Client: CLIENT007;
Where CLIENT007 would never have had a Job 2.
Edited by Kristoff1875, 28 December 2012 - 07:53 PM.
#4
Posted 28 December 2012 - 08:00 PM
SELECT MAX(jobidfield) FROM jobs WHERE clientid = 7
Store the IDs as numeric only.
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
#5
Posted 28 December 2012 - 08:06 PM
"INSERT INTO
Jobs (ClientID, JobNumber)
values
'{$_POST['ClientID']}',
'{$_POST['JobNumber']}'"
And simply adding the job number in a field, so i'm just trying to simplify that part slightly so it automatically gives me the next number for the JobNumber field for that ClientID
(I have changed that code slightly as I actually select a field from the database to find the ClientID)
Infact, just thinking about it, is it possible to find the highest number in a column where ClientID = ClientID and then to use a + 1 value in PHP? Just thinking I have a dropdown of all the Client's and then could pre-populate the JobID field on select of the Client dropdown, with the next number before I send it to the form?
Edited by Kristoff1875, 28 December 2012 - 08:12 PM.
#6
Posted 28 December 2012 - 08:24 PM
SELECT MAX(jobidfield) FROM jobs WHERE clientid = 7
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
#7
Posted 28 December 2012 - 08:30 PM
#8
Posted 28 December 2012 - 08:31 PM
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
#9
Posted 28 December 2012 - 08:36 PM
If so how would I I go about echoing the value in a field, but + 1?
#10
Posted 28 December 2012 - 08:40 PM
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
#11
Posted 28 December 2012 - 08:52 PM
|baaChart| easy line, column and pie charts
|baaSelect| generate js and php code for dynamic linked dropdowns
#12
Posted 28 December 2012 - 08:53 PM
#13
Posted 28 December 2012 - 08:57 PM
How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq
Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!
#14
Posted 28 December 2012 - 09:04 PM
Edited by Kristoff1875, 28 December 2012 - 09:04 PM.
#15
Posted 29 December 2012 - 09:06 AM
That's one of the reasons you should consider redoing that system of yours. You could always find out what number in line a certain job was, if you have either the customer ID or the job ID, by simply counting the previous jobs in front of it.
It's a lot harder to figure out which customer the order above belongs to, if all customers have a job with ID 1.
#16
Posted 29 December 2012 - 09:50 PM
Edited by Kristoff1875, 29 December 2012 - 09:50 PM.
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users












