Hi,
I desperately need some help here. Can anybody tell me how is it possible to check the last value of a field and then increment that value by one and then insert the incremented-value into that field?
Example
Table:Books
Book id - Name - Purchase Year - Accession Number
1 - x - 2000 - 1.2000
2 - y - 2010 - 1.2010
3 - z - 2004 - 1.2004
Now, with the help of a form, the user input the book name and purchase year ( for eg. a,2010). Then a checking needs to be done to get the last value of the accession number field where accession number = ((digit).2010), then add one to that value and gets inserted into the field. So the table becomes.
Book id - Name - Purchase Year - Accession Number
1 - x - 2000 - 1.2000
2 - y - 2010 - 1.2010
3 - z - 2004 - 1.2004
4 - a - 2010 - 2.2010
Please tell me if there is any confusion as I need this solution urgently...Thank you
Check last value of a field and then increment it by one
Moderator: General Moderators
Re: Check last value of a field and then increment it by one
I think you can SELECT every thing from the table where the year is equal to desired year then check the number of rows (You can use the function mysql_num_rows() )returned for example if there are 4 records where the year equal to 2010 for example add 1 to this number and append it.
Re: Check last value of a field and then increment it by one
And what happens if two people try to do this at the same time? Person A gets 2.2010, person B gets 2.2010, person A inserts value, person B inserts value.
Re: Check last value of a field and then increment it by one
No, there will be only one user for inserting details...and that's for certaintasairis wrote:And what happens if two people try to do this at the same time? Person A gets 2.2010, person B gets 2.2010, person A inserts value, person B inserts value.
Re: Check last value of a field and then increment it by one
Thank you...I will definately try this...kalidG wrote:I think you can SELECT every thing from the table where the year is equal to desired year then check the number of rows (You can use the function mysql_num_rows() )returned for example if there are 4 records where the year equal to 2010 for example add 1 to this number and append it.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Re: Check last value of a field and then increment it by one
I believe he is talking about a possible race condition.rbhoumik wrote:No, there will be only one user for inserting details...and that's for certaintasairis wrote:And what happens if two people try to do this at the same time? Person A gets 2.2010, person B gets 2.2010, person A inserts value, person B inserts value.